初级---开发中英文互译功能

首页

 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'
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值