天天看點

python+urllib+爬蟲的有道翻譯

簡單的post請求爬蟲

我們需要模拟好請求頭:

python+urllib+爬蟲的有道翻譯

做好post請求的form資料:

python+urllib+爬蟲的有道翻譯

輸入關鍵字:

python+urllib+爬蟲的有道翻譯

最後測試:

python+urllib+爬蟲的有道翻譯

代碼:

# coding=utf-8

import urllib
import urllib2

#
# http://fanyi.youdao.com/


url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
key = raw_input('請輸入翻譯的文字:')
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    }
form_data = {
    'i': key,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': '1540194102029',
    'sign': '4cd7449ce8df95d131f863ce54174248',
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTIME',
    'typoResult': 'false'
}

data = urllib.urlencode(form_data)
# 如果Request()方法裡的data參數有值,那麼這個請求就是POST
# 如果沒有,就是Get
request = urllib2.Request(url, data=data, headers=headers)

print urllib2.urlopen(request).read()
           

爬取拉勾網資訊參考:https://blog.csdn.net/qq_35723619/article/details/83176329