python版本3.6
根据文字获取有道词典中英文翻译结果
import urllib.request import urllib.parse import json #获取文字翻译结果 def get_data(words): data = {} data["type"] = "AUTO" data["i"] = words 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') return data #根据url获取网页源码 def url_open(url, data): req = urllib.request.Request(url, data) req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36") response = urllib.request.urlopen(req) html = response.read() html = html.decode("utf-8") return html def get_json_data(html): result = json.loads(html) result = result['translateResult'] result = result[0][0]['tgt'] return result #获取翻译结果 def getWordsTranslation(words): url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict.top" data = get_data(words) html = url_open(url, data) result = get_json_data(html) return result #测试 data=getWordsTranslation("测试") print(data)
测试结果: