输入英文从有道翻译网址爬取js数据
#coding=utf-8
import urllib
import urllib2
fanyi_url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
key = raw_input('请输入需要翻译的文字')
formData = {
'i': key,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'sign': 'e2fd5830da31a783b6c1f83b522a7d7c',
'doctype': 'json',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CLICKBUTTION',
'typoResult': 'true',
}
data = urllib.urlencode(formData)
request = urllib2.Request(fanyi_url, data=data, headers=head)
resp = urllib2.urlopen(request)
print resp.read()
-----------------------------The results-------------------------------
请输入需要翻译的文字hello world
{"type":"EN2ZH_CN","errorCode":0,"elapsedTime":1,"translateResult":[[{"src":"hello world","tgt":"你好,世界"}]]}
---------------------------------------------------------------------------
通过json.cn翻译js数据
{
"type":"EN2ZH_CN",
"errorCode":0,
"elapsedTime":2,
"translateResult":[
[
{
"src":"hello world",
"tgt":"你好,世界"
}
]
]
}