爬虫仅供个人练习交流使用
有道现在网页的url有反爬虫,需要用到下文中的url
import requests
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
while True:
content = input('请输入需要翻译的内容(输入q退出程序):')
if content == 'q':
break
data = {}
data['i'] = content
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '1516759474127'
data['sign'] = '63045e22eedd2ca1704165e314dbfa6b'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTIME'
data['typoResult'] = 'false'
response = requests.post(url,headers=headers,data=data)
#print(response.text)
dictTrans = response.json()
print('翻译的结果是:%s'%(dictTrans['translateResult'][0][0]['tgt']))
测试如图: