给Flask加上百度翻译功能,这样可以用中文进行搜索了
2023-12-13 14:57:51
上一篇博客:Flask之手搓bootstrap翻页-CSDN博客?里,对?OMDb API - The Open Movie Database?的搜索,只能使用英文,才能搜索出电影信息,如果使用中文,是搜索不到结果的。这里就需要使用翻译,把中文电影名翻译成英文电影名。
这里使用百度翻译,百度翻译量少的话是免费的,百度翻译开放平台,而且有python的示例代码,可以直接照抄。
# -*- coding: utf-8 -*-
# This code shows an example of text translation from English to Simplified-Chinese.
# This code runs on Python 2.7.x and Python 3.x.
# You may install `requests` to run this code: pip install requests
# Please refer to `https://api.fanyi.baidu.com/doc/21` for complete api document
import requests
import random
import json
from hashlib import md5
# Set your own appid/appkey.
appid = 'INPUT_YOUR_APPID'
appkey = 'INPUT_YOUR_APPKEY'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
query = 'Hello World! This is 1st paragraph.\nThis is 2nd paragraph.'
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
# Show response
print(json.dumps(result, indent=4, ensure_ascii=False))
只需要在搜索路由里,将form表单传递过来的中文搜索词翻译成英文即可。如果是英文,那就让它再翻译一遍,一般是没有变化。
详细代码如下。
app.py
from flask import Flask, render_template, request
import requests
import math
import json
import random
from hashlib import md5
app = Flask(__name__)
@app.route('/', methods=["GET"])
# @app.route('/?s=<s>&page=<page>', methods=["GET"])
def main():
s = request.args.get("s")
page = request.args.get("page")
if page is None:
page = 1
else:
page = int(request.args.get("page"))
if s is None:
s = 'superman'
# Here is your key: 4ee0241e
# OMDb API: http://www.omdbapi.com/?i=tt3896198&apikey=4ee0241e
# rawData = requests.get("http://www.omdbapi.com/?apikey=4ee0241e&s=batman&page=1")
rawData = requests.get(f"http://www.omdbapi.com/?apikey=4ee0241e&s={s}&page={page}")
movies = rawData.json()
if movies["Response"] == "True":
pages = math.ceil(int(movies["totalResults"]) / 10)
else:
pages = 0
data = dict(
movies=movies,
active1="active",
active2="",
pages=pages,
page=page,
s=s,
)
return render_template("index.html", data=data)
@app.route('/', methods=["POST"])
def search():
s = request.form["name"]
en_s = fanyi(s)
page = 1
rawData = requests.get(f"http://www.omdbapi.com/?apikey=4ee0241e&s={en_s}&page={page}")
movies = rawData.json()
if movies["Response"] == "True":
pages = math.ceil(int(movies["totalResults"]) / 10)
else:
pages = 0
data = dict(
movies=movies,
active1="active",
active2="",
pages=pages,
page=page,
s=s,
)
return render_template("index.html", data=data)
def fanyi(s):
# Set your own appid/appkey.
appid = 'INPUT_YOUR_APPID'
appkey = 'INPUT_YOUR_APPKEY'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'zh'
to_lang = 'en'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
# query = 'Hello World! This is 1st paragraph.\nThis is 2nd paragraph.'
query = s
# Generate salt and sign
def make_md5(sm, encoding='utf-8'):
return md5(sm.encode(encoding)).hexdigest()
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
# Show response
# print(json.dumps(result["trans_result"][0]["dst"], indent=4, ensure_ascii=False))
# print(json.dumps(result, indent=4, ensure_ascii=False)) # "superman"
# print(result["trans_result"][0]["dst"]) # superman
return result["trans_result"][0]["dst"]
@app.route('/<imdbID>')
def movie_by_title(imdbID):
# rawData = requests.get("http://www.omdbapi.com/?apikey=4ee0241e&i={}".format(imdbID))
rawData = requests.get(f"http://www.omdbapi.com/?apikey=4ee0241e&i={imdbID}")
movie = rawData.json()
data = dict(
movie=movie,
active1="",
active2="active",
)
return render_template("movie.html", data=data)
if __name__ == '__main__':
app.run(debug=True)
footer.html
<div class="mt-3">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
</div>
header.html
<meta charset="UTF-8">
<title>母版</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
nav.html
<div class="contain bg-body-tertiary">
<nav class="navbar navbar-expand-lg container">
<div class="container-fluid">
<a class="navbar-brand">电影</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item {{ data["active1"] }}">
<a class="nav-link" aria-current="page" href="/">首页</a>
</li>
<li class="nav-item {{ data["active2"] }}">
<a class="nav-link" href="javascript:(0)">详情</a>
</li>
</ul>
<form class="d-flex" role="search" method="post" action="{{ url_for('search') }}">
<input class="form-control me-2" type="search" name="name" placeholder="搜索影片" value="{{ data.s }}"
aria-label="Search">
<button class="btn btn-outline-success" type="submit">search</button>
</form>
</div>
</div>
</nav>
</div>
home.html
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
{% include './common/header.html' %}
{% endblock %}
<style>
.card:hover {
box-shadow: 10px 5px 5px #ccc;
}
.active {
background: rgb(208, 216, 222);
}
.navbar {
--bs-navbar-padding-y: 0;
}
.nav-link {
padding: 20px;
}
.row > * {
padding-left: 0;
}
</style>
</head>
<body>
{% block nav %}
{% include './common/nav.html' %}
{% endblock %}
<div class="container mt-3">
<div class="row">
{% block content %}
{% endblock %}
</div>
</div>
{% block footer %}
{% include './common/footer.html' %}
{% endblock %}
</body>
</html>
movie.html
{% extends "common/home.html" %}
{% block content %}
<div class="d-flex align-items-center justify-content-center">
<div class="card mb-3" style="max-width: 1080px;">
<div class="row">
<div class="col-md-4">
<img src="{{ data["movie"].Poster }}" class="img-fluid rounded-start" alt="...">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{ data["movie"].Title }}</h5>
<p class="card-text">Year:{{ data["movie"].Year }}</p>
<p class="card-text">Runtime:{{ data["movie"].Runtime }}</p>
<p class="card-text">Actors:{{ data["movie"].Actors }}</p>
<p class="card-text">{{ data["movie"].Actors }}</p>
<p class="card-text">{{ data["movie"].Year }}</p>
<p class="card-text"><small class="text-body-secondary">{{ data["movie"].Plot }}</small></p>
</div>
<div class="d-flex align-items-center justify-content-center">
<a href="{{ url_for("main") }}" class="btn btn-success">返回</a>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
index.html
{% extends "common/home.html" %}
{% block content %}
{% for movie in data["movies"].Search %}
<div class="col text-center mt-3">
<div class="card" style="height: 510px;overflow: hidden;width: 240px;">
<img src="{{ movie.Poster }}" height="350" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title" style="height: 48px;">{{ movie.Title[:35] }}</h5>
<p class="card-text">{{ movie.Year }}</p>
<a href="/{{ movie.imdbID }}" class="btn btn-primary">详情</a>
</div>
</div>
</div>
{% endfor %}
{# 翻页 #}
<div class="d-flex align-items-center justify-content-center">
<div class="mt-3 mb-5">
<nav aria-label="Page navigation example">
{% if data.pages>0 %}
<ul class="pagination pagination-lg justify-content-center">
<li class="page-item {{ 'disabled' if data.page==1 else '' }}">
<a class="page-link" href="/?s={{ data.s }}&page={{ data.page - 1 }}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% if data.pages<8 %}
{% for i in range(data.pages) %}
<li class="page-item"><a
class="page-link {{ 'disabled' if data.page == i + 1 else '' }}"
href="/?s={{ data.s }}&page={{ i + 1 }}">
{{ i + 1 }}</a>
</li>
{% endfor %}
{% elif data.pages>=8 %}
{% for i in range(3) %}
{% if data.pages - 5 > data.page %}
<li class="page-item"><a
class="page-link {{ 'disabled' if data.page == i + data.page else '' }}"
href="/?s={{ data.s }}&page={{ i + data.page }}">
{{ i + data.page }}</a>
</li>
{% else %}
<li class="page-item"><a
class="page-link {{ 'disabled' if data.page == i + data.pages - 5 else '' }}"
href="/?s={{ data.s }}&page={{ data.pages - 5 + i }}">
{{ i + data.pages - 5 }}</a>
</li>
{% endif %}
{% endfor %}
<li class="page-item}">
<a class="page-link" href="javascript:(0)">
<span aria-hidden="true">...</span>
</a>
</li>
{% for i in range(3)[::-1] %}
<li class="page-item"><a
class="page-link {{ 'disabled' if data.page == data.pages - i else '' }}"
href="/?s={{ data.s }}&page={{ data.pages - i }}">
{{ data.pages - i }}</a>
</li>
{% endfor %}
{% endif %}
<li class="page-item {{ 'disabled' if data.page==data.pages else '' }}">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
{% else %}
搜索: <span class="text-warning">{{ data.s }}</span> 的结果为空!
{% endif %}
</nav>
</div>
</div>
{% endblock %}
index.html模板里,加上了没有结果时的代码:
{% else %}
搜索: <span class="text-warning">{{ data.s }}</span> 的结果为空!
{% endif %}
把数字放前面,反而可以搜到,不知道翻译是咋翻译的。
?
文章来源:https://blog.csdn.net/andux/article/details/134879620
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!