python爬取有道翻译
使用依赖库
import urllib.request
import urllib.parse
import json
源代码
import urllib.request
import urllib.parse
import json
#coding:utf-8
content = input("请输入要翻译的内容: ")
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
data = {}
data['type'] = "AUTO"
data['i'] = content
data['doctype'] = "json"
data['version'] = "2.1"
data['keyfrom'] = "fanyi.web"
data['ue'] = "UTF-8"
data['typoResult'] = "True"
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8')
#print(html)
target = json.loads(html)
#print(target)
print("翻译的结果为: %s" % (target['translateResult'][0][0]['tgt']))
使用python3运行