1.重要过程
2.完整代码
import requests as req
import time
import random
import hashlib
class YDSpider():
"""有道翻译"""
def crawl(self, src_text):
url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
headers={
"Accept": "application/json, text/javascript, */*; q=0.01"
, "Accept-Encoding": "gzip, deflate, br"
, "Accept-Language": "zh,zh-CN;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6"
, "Connection": "keep-alive"
, "Content-Length": "245"
, "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
, "Cookie": "OUTFOX_SEARCH_USER_ID=-1464289522@10.108.160.105; JSESSIONID=aaaopIy3ZYGbL-2a-hyPx; OUTFOX_SEARCH_USER_ID_NCOO=1343267741.5045633; fanyi-ad-id=112781; fanyi-ad-closed=1; ___rl__test__cookies=1624977496478"
, "Host": "fanyi.youdao.com"
, "Origin": "https://fanyi.youdao.com"
, "Referer": "https://fanyi.youdao.com/"
, "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
, "X-Requested-With": "XMLHttpRequest"
}
"""
# 网页中的js生成规则
var t = n.md5(navigator.appVersion)
, r = "" + (new Date).getTime()
, i = r + parseInt(10 * Math.random(), 10);
return {
ts: r,
bv: t,
salt: i,
sign: n.md5("fanyideskweb" + e + i + "Tbh5E8=q6U3EXe+&L[4c@")
}
"""
ts = str(time.time()*1000)
salt = ts + str(random.randint(0, 10))
md5 = hashlib.md5()
enc_str = "fanyideskweb" + src_text + salt + "Tbh5E8=q6U3EXe+&L[4c@"
md5.update(enc_str.encode( 'utf-8' ))
sign = md5.hexdigest()
data = {
'i' : src_text,
'from' : 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign' : sign,
'ts' : ts,
'bv' : '6ba427a653365049d202e4d218cbb811',
'doctype' : 'json',
'version' : '2.1',
'keyfrom' : 'fanyi.web',
'action' : 'FY_BY_REALT1ME'
}
resp = req.post(url, headers=headers, data=data)
content = resp.content.decode("utf-8")
print(content)
if __name__ == "__main__":
spider = YDSpider()
spider.crawl("hi there")
spider.crawl("the moon won't come for you")