有道翻譯爬蟲實作
POST請求:
#有道翻譯爬蟲 from urllib import request import urllib import re #構造請求頭資訊 header={ "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) \ AppleWebKit/537.36 (KHTML, like Gecko) \ Chrome/81.0.4044.122 Safari/537.36" }#谷歌瀏覽器 #http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule 網頁上的url url="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" key="白金之星,世界" #發送到web服務器的表單資料 formdata=https://www.cnblogs.com/Just-a-calm-programmer/p/{ "i":key, "from":"AUTO", "to":"AUTO", "smartresult":"dict", "client":"fanyideskweb", "salt":"15880563488791", "sign":"cc2c40d740538fc5edc0380891faef27", "ts":"1588053583943", "bv":"f9c86b1fdf2f53c1fefaef343285247b", "doctype":"json", "version":"2.1", "keyfrom":"fanyi.web", "action":"FY_BY_REALTlME" } #經過urlencode轉碼 data=https://www.cnblogs.com/Just-a-calm-programmer/p/urllib.parse.urlencode(formdata).encode(encoding='utf-8') #如果Request()方法中的data引數有值,那么這個請求就是POST #如果沒有,就是GET req=request.Request(url,data=https://www.cnblogs.com/Just-a-calm-programmer/p/data,headers=header) response=request.urlopen(req).read().decode() #print(response) # "tgt":"Platinum stars, and the world"}]] 提取Platinum stars, and the world pat=r'"tgt":"(.*?)"}]]' #字串中有"",再用''括起來表示字串 result=re.findall(pat,response) print("翻譯結果:"+result[0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152954.html
標籤:Python
下一篇:Flask基礎知識
