import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
while True:
content = input("请输入要翻译的内容:")
data = {
'i': content,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': '15496171372804',
'sign': 'b53bcf93a7982b8057077b3f1967e2db',
'ts': '1549617137280',
'bv': 'bbb3ed55971873051bc2ff740579bb49',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
'typoResult': 'false',
}
data = urllib.parse.urlencode(data).encode('utf-8') #将编码编成'utf-8'的形式
response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8') #将文件编码解码成'utf-8'形式
html = json.loads(html) #得到的json格式编码 用json内置函数进行解码
print(html['translateResult'][0][0]['tgt']) #通过解码内容来写输出
print("-------------------------------------")
print("-------------------------------------")
python爬虫-有道翻译
最新推荐文章于 2023-07-04 16:26:37 发布