码迷,mamicode.com
首页 > 其他好文 > 详细

假期十三

时间:2020-02-14 00:39:29      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:tput   name   import   pen   client   from   rom   quit   put   

from urllib import request
from urllib import parse
import json
import time


def translation(string):
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
headers = {
‘User-Agent‘: ‘Mozilla/5.0 (X11; Linux x86_64) ‘
‘AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36‘}
data = {
‘i‘: string,
‘from‘: ‘AUTO‘,
‘to‘: ‘AUTO‘,
‘smartresult‘: ‘dict‘,
‘client‘: ‘fanyideskweb‘,
‘salt‘: ‘15805516827176‘,
‘sign‘: ‘17cf93ee9a11894806209a83c480ca2b‘,
‘ts‘: ‘1580551682717‘,
‘bv‘: ‘334720d41a1d174718af3a3d58d56f23‘,
‘doctype‘: ‘json‘,
‘version‘: ‘2.1‘,
‘keyfrom‘: ‘fanyi.web‘,
‘action‘: ‘FY_BY_CLICKBUTTION‘}
data = parse.urlencode(data).encode("utf-8")
req = request.Request(url, data, headers)
response = request.urlopen(req)
html = response.read().decode("utf-8")
prasewords(html)


def prasewords(html):
trans = json.loads(html)
firstwords = trans[‘translateResult‘][0][0][‘src‘]
finaltrans = trans[‘translateResult‘][0][0][‘tgt‘]
output(firstwords, finaltrans)


def output(firstwords, finaltrans):
print("您输入的是:%s" % firstwords)
print("翻译结果是:%s" % finaltrans)


def main():
while True:
print("")
string = input("请输入要翻译的内容(输入quit或q或exit均可退出程序):")
if string == ‘quit‘ or string == ‘q‘ or string == ‘exit‘:
break

translation(string)
time.sleep(3)


if __name__ == ‘__main__‘:
main()

假期十三

标签:tput   name   import   pen   client   from   rom   quit   put   

原文地址:https://www.cnblogs.com/jbwen/p/12305780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!