Python 有道翻译 爬虫 有道翻译API 突破有道翻译反爬虫机制

时间:2021-02-10 16:37:42

py2.7

 1 #coding: utf-8
 2 import time
 3 import random
 4 import hashlib
 5 import requests
 6 
 7 
 8 while(1):
 9     url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom='
10 
11     content = raw_input('输入>>>: ')
12 
13     s = "AUTO",
14     l = "AUTO"
15     u = 'fanyideskweb'
16     c = 'rY0D^0\'nM0}g5Mm1z%1G4'
17     d = content
18     f = str(int(time.time()*1000)+random.randint(1,10))
19     sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
20 
21     headers = {
22     'Accept':'application/json, text/javascript, */*; q=0.01',
23     'Accept-Encoding':'gzip, deflate',
24     'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
25     'Connection':'keep-alive',
26     'Content-Length':'205',
27     'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
28     'Cookie':'JSESSIONID=aaalHNVSigPD8-hsnhf3v; SESSION_FROM_COOKIE=fanyiweb; OUTFOX_SEARCH_USER_ID=526401539@113.16.65.153; _ntes_nnid=1892114ba72ae7f868a29a4db02914a0,1502250589343; _dict_cpm_show=1502250589350; _dict_cpm_close=1; OUTFOX_SEARCH_USER_ID_NCOO=1688640113.572293; ___rl__test__cookies=1502251640921',
29     'Host':'fanyi.youdao.com',
30     'Origin':'http://fanyi.youdao.com',
31     'Referer':'http://fanyi.youdao.com/',
32     'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
33     'X-Requested-With':'XMLHttpRequest',
34     }
35 
36     data = {}
37     data['i']=content
38     data['from']=s
39     data['to']=l
40     data['smartresult']='dict'
41     data['client']='fanyideskweb'
42     data['salt']=f
43     data['sign']=sign
44     data['doctype']='json'
45     data['version']='2.1'
46     data['keyfrom']='fanyi.web'
47     data['action']='FY_BY_CLlCKBUTTON'
48     data['typoResult']='true'
49 
50     res = requests.post(url, data, headers=headers)
51     print res.text