Python爬虫|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去掉,而且这样的请求只能是用于英文翻译汉文.
"""
from urllib import request
from urllib import parse
import json
if __name__ == "__main__":
#对应上图的 Request URL 为避免{"errorCode":50}的错误,去除 url 中的_o
#Request_URL = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
Request_URL = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
#创建 Form_Data 字典,存储上图的 Form Data
Form_Data = https://www.it610.com/article/{}
Form_Data['i'] = 'Tom'
Form_Data['from'] = 'AUTO'
Form_Data['to'] = 'AUTO'
Form_Data['smartresult'] = 'dict'
Form_Data['client'] = 'fanyideskweb'
Form_Data['salt'] = '1526796477689'
Form_Data['sign'] = 'd0a17aa2a8b0bb831769bd9ce27d28bd'
Form_Data['doctype'] = 'json'
Form_Data['version'] = '2.1'
Form_Data['keyfrom'] = 'fanyi.web'
Form_Data['action'] = 'FY_BY_REALTIME'
Form_Data['typoResult'] = 'false'
#使用 urlencode 方法转换标准格式
data = https://www.it610.com/article/parse.urlencode(Form_Data).encode('utf-8')
head = {}
# 写入 User Agent 信息
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5792.400 QQBrowser/10.2.2101.400'
# 创建 Request 对象
req = request.Request(Request_URL, headers=head)
# 传递 Request 对象和转换完格式的数据
response = request.urlopen(req, data=https://www.it610.com/article/data)
# 读取信息并解码
html = response.read().decode('utf-8')
# 使用 JSON
translate_results = json.loads(html)
# 找到翻译结果
translate_results = translate_results['translateResult'][0][0]['tgt']
# 打印翻译信息
【Python爬虫|Python爬虫 ————POST请求有道翻译{"errorcode":50}】print("翻译的结果是: %s" % translate_results)

    推荐阅读