中英翻译
'''
中英翻译
'''
from urllib import request, parse
import random
import json
ua_list = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201']
def loadPage():
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule "
UserAgent = random.choice(ua_list)
headers = {
"User-Agent": UserAgent
}
req = request.Request(url, headers=headers)
i = str(input('请输入要翻译的单词:'))
data = {
"i": i,
"from": "zh-CHS",
"to": "ja",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": "15681711794211",
"sign": "e19888d8b3b8700a6adbd5ce4712a567",
"ts": "1568171179421",
"bv": "dbbd037970d0424cdfdc91fa162c9f40",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTlME",
}
data = parse.urlencode(data).encode("utf-8")
content = request.urlopen(req, data=data)
content = content.read().decode("utf-8")
ret = json.loads(content)
text = ret["translateResult"][0][0]
print('翻译结果:' + text['tgt'])
if __name__ == "__main__":
loadPage()
总结
- 常见的User-Agent大全
- 耐心观察