爬虫(POST)——有道翻译(有bug)

工具:python3

过程:抓包得到有道翻页面的url;复制post请求头,得到headers中的信息;复制post请求的body,得到formdata中的信息。构造post请求,返回响应

import urllib.request

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
    "Content-Type": " application/x-www-form-urlencoded",
    "X-Requested-With": "XMLHttpRequest"
           }

url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule "

key = input("请输入需要翻译的文字:")

formdata = {
    "i": key,
    "from":"AUTO",
    "to":"AUTO",
    "smartresult":"dict",
    "client":"fanyideskweb",
    "salt":"1527240517613",
    "sign":"b6871f33bfa94784d832dddbe9fba785",
    "doctype":"json&version=2.1",
    "keyfrom":"fanyi.web",
    "action":"FY_BY_REALTIME",
    "typoResult":"false"
}


data = urllib.parse.urlencode(formdata)
data = bytes(data, encoding="utf8")
print(data)
request = urllib.request.Request(url, data=data, headers = headers)
print(request)
response = urllib.request.urlopen(request)
print(response.read())

返回error:50 

修改url,去掉_o后仍然不能得到正确的信息,,,原来,有道有反爬虫机制!

哈哈哈哈,刚学爬虫,把这个问题先放一下好啦!

我很棒棒哦!

posted @ 2018-05-28 20:50  高圈圈  阅读(332)  评论(0编辑  收藏  举报