Python爬虫 ————POST请求有道翻译{"errorcode":50}

话不多说直接上链接(http://blog.csdn.net/nunchakushuang/article/details/75294947)
因为有道翻译有反爬虫机制,所以简单的爬肯定不行,但是这一篇博客只是告诉我们有道的JS反爬虫代码,完全运行后还需要改你得到的POST请求的URL
我的URL:http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
需要修改成http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule
就是把_o去掉,而且这样的请求只能是用于英文翻译汉文,如果有大牛知道另外的完全的方法
请别忘了告诉我一声哈…………_
附上我的代码

#!/usr/bin/python
#  coding: utf-8
import urllib2
import urllib
import json
import time
import random
import hashlib

url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
headers = {
"Accept":" application/json, text/javascript, */*; q=0.01",
"X-Requested-With":" XMLHttpRequest",
"User-Agent":" Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Content-Type":" application/x-www-form-urlencoded; charset=UTF-8",
"Accept-Language":" zh-CN,zh;q=0.9"

}

key = raw_input("translation for:  ")

u = 'fanyideskweb'
d = key
f = str(int(time.time()*1000) + random.randint(1,10))
c = 'ebSeFb%=XZ%T[KZ)c(sy!'

md5 = hashlib.md5()
md5.update(u)
md5.update(d)
md5.update(f)
md5.update(c)
sign = md5.hexdigest()
data = {
"i":key,
"from":"AUTO",
"to":"AUTO",
"smartresult":"dict",
"client":"fanyideskweb",
"salt":f,
"sign":sign,
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_REALTIME",
"typoResult":"false"
}
data = urllib.urlencode(data)
request = urllib2.Request(url,data=data,headers=headers)
respose = urllib2.urlopen(request)
line = json.load(respose)
print line['translateResult'][0][0]['tgt']

参考:https://blog.csdn.net/ISxiancai/article/details/79349184

你可能感兴趣的:(Python爬虫 ————POST请求有道翻译{"errorcode":50})