有道翻譯爬取程式碼
阿新 • • 發佈:2018-12-11
import urllib.request import urllib.parse #通過抓包的方式獲取post的url,並不是瀏覽器位址列的url url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" #使用者介面輸入 key=input("請輸入需要翻譯的文字:") #傳送到web伺服器的表單資料 formdata ={ "i": key, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "client": "fanyideskweb", "salt": "1537698317221", "sign": "b569c8bf54bf1b0d71725726e7c1a5", "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_REALTIME", "typoResult": "false" } #headers headers={ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"} #經過urlencode轉碼 data = urllib.parse.urlencode(formdata).encode(encoding='gbk') #如果Request()方法裡的data引數裡有值,那麼這個請求就是post #如果沒有,則是get請求 request = urllib.request.Request(url,data=data, headers = headers) repost = urllib.request.urlopen(request) html=repost.read().decode("utf-8") print(html)