首页
main函数部分代码
from flask import Flask,render_template,request # 导入包中的一个工具
import requests
# flask对象
app = Flask(__name__,template_folder="templates")
@app.route("/")
def hello_baidu(): # /指根目录 def指自己创建功能 @是装饰器
return render_template("index.html")
@app.route("/fanyi")
def fanyi():
return render_template("fanyi.html")
@app.route("/tran")
def tran():
kd=request.args.get("kd")
print(kd)
res=requests.post("https://aidemo.youdao.com/trans",data={"q":kd.strip()})
print(res)
ret=res.json()['basic']['explains'][0]
print(ret)
return render_template("fanyi.html",**{"kd" : kd,"ret" : ret})
app.run()
创建index.html
<!--文档类型说明-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>英语学习网</title>
</head>
<!--body中的内容显示给用户-->
<body>
<div>
<img src="https://ts4.cn.mm.bing.net/th?id=OIP-C.ErSUxd-oUB_zG5XHQnnEpgHaFL&w=298&h=209&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2"
height="300" width="100%" alt="加载失败">
</div>
<div style="display:inline-block">
<a href="/fanyi">
<p>在线编辑</p>
<img src="https://ms.bdimg.com/pacific/0/pic/-1515285191_777292561.png?x=0&y=0&vh=150.00&vw=242.00&oh=150.00&ow=242.00&w=146&h=91&rs=0"
alt="加载失败" height="300" width="300">
</a>
</div>
<div style="display:inline-block">
<a href="">
<p>背单词</p>
<img src="https://ts3.cn.mm.bing.net/th?id=OIP-C.pgcC7vAHyn6cRjHt-5CdygHaHa&w=250&h=250&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2"
alt="加载失败" height="300" width="300">
</a>
</div>
</body>
</head>
</html>
创建fanyi.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>翻译</title>
</head>
<body>
<p>在线翻译</p>
<form action="/tran">
<textarea name="kd" rows="10",cols="20">
{{ kd }}
</textarea>
<input type="submit" value="点击翻译"></input>
</form>
<textarea rows="10",cols="20">
{{ ret }}
</textarea>
</body>
</html>
由服务器端返回的test.json
ctrl+alt+L可以实现python代码缩进
{
'returnPhrase': [
'apple'
],
'query': 'Apple',
'errorCode': '0',
'l': 'en2zh-CHS',
'tSpeakUrl': 'https://openapi.youdao.com/ttsapi?q=%E8%8B%B9%E6%9E%9C&langType=zh-CHS&sign=0348BBCC75C5A1885C2992E536D1C1C7&salt=1671093145033&voice=4&format=mp3&appKey=2423360539ba5632&ttsVoiceStrict=false',
'web': [
{
'value': [
'苹果公司'
],
'key': 'Apple'
},
{
'value': [
'苹果公司',
'美国苹果公司',
'苹果'
],
'key': 'apple inc'
},
{
'value': [
'大苹果',
'纽约',
'大苹果城'
],
'key': 'BIG APPLE'
}
],
'requestId': '9a992b1c-959f-4733-8e60-bbd322fa3019',
'translation': [
'苹果'
],
'dict': {
'url': 'yddict://m.youdao.com/dict?le=eng&q=Apple'
},
'webdict': {
'url': 'http://mobile.youdao.com/dict?le=eng&q=Apple'
},
'basic': {
'exam_type': [
'初中',
'高中',
'CET4',
'CET6',
'考研'
],
'us-phonetic': 'ˈæp(ə)l',
'phonetic': 'ˈæp(ə)l',
'uk-phonetic': 'ˈæp(ə)l',
'wfs': [
{
'wf': {
'name': '复数',
'value': 'apples'
}
}
],
'uk-speech': 'https://openapi.youdao.com/ttsapi?q=Apple&langType=en&sign=1FD318E4F3365A98F3C0C7D4261C541A&salt=1671093145033&voice=5&format=mp3&appKey=2423360539ba5632&ttsVoiceStrict=false',
'explains': [
'n. 苹果'
],
'us-speech': 'https://openapi.youdao.com/ttsapi?q=Apple&langType=en&sign=1FD318E4F3365A98F3C0C7D4261C541A&salt=1671093145033&voice=6&format=mp3&appKey=2423360539ba5632&ttsVoiceStrict=false'
},
'isWord': True,
'speakUrl': 'https://openapi.youdao.com/ttsapi?q=Apple&langType=en&sign=1FD318E4F3365A98F3C0C7D4261C541A&salt=1671093145033&voice=4&format=mp3&appKey=2423360539ba5632&ttsVoiceStrict=false'
}