1. 程式人生 > >有道翻譯——爬取

有道翻譯——爬取

輸入英文從有道翻譯網址爬取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":"你好,世界"
            }
        ]
    ]
}