廢話不多說,直接上代碼
import urllib.request
import urllib.parse
import json
content = input("請輸入要翻譯的内容:")
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
data = {}
data['type'] = 'AUTO'
data['i'] = content
data['doctype'] = 'json'
data['xmlVersion'] = '1.8'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['action'] = 'FY_BY_CLICKBUTTON'
data['typoResult'] = 'true'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url,data)
html = response.read().decode('utf-8')
target = json.loads(html)
print("翻譯結果:%s" % (target['translateResult'][0][0]['tgt']))
友情提示:不要多用,會被屏蔽的哈(博友們可以出個招,看看怎麼隐藏自己,不被屏蔽)
轉載于:https://blog.51cto.com/402753795/1793631