本帖最后由 wushaominkk 于 2019-5-19 19:43 编辑
[Python] 纯文本查看 复制代码 import urllib.request as ub
import urllib.parse as parse
import time
import random
from hashlib import md5
class youdaoSpider(object):
def __init__(self,translateWord):
self.translateWord=translateWord
self.url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
self.data={} #存储发送的数据
self.headers={} #存储HTTP协议的报头
def get_md(self,value):
'''
给字符串数据进行md5加密 然后
:param value: 要加密的md5字符串
:return:
'''
m=md5()
m.update(value.encode("utf-8"))
return m.hexdigest()
def get_sign(self,salt):
"""
获取sign
:param salt:md5加密的盐值
:return:
"""
value="fanyideskweb" + self.translateWord + salt + "p09@Bn{h02_BIEe]$P^nG"
return self.get_md(value)
def get_salt(self,now_t):
"""
获取要md5加密需要的盐值
:param now_t:现在的事件截
:return:
"""
return str(now_t+ random.randint(0, 10))
def sendPost(self):
"""
发送POST请求
:return:
"""
nowtime=int(time.time() * 1000)
salt=self.get_salt(nowtime)
sign=self.get_sign(salt)
print(salt)
print(sign)
self.data={
"i":self.translateWord,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": salt,
"sign": sign,
"ts": nowtime,
"bv": "803c04ba5e71dce4e8b081b65a2d3f5b",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME",
"typoResult": "false",
}
self.headers={
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36",
"Cookie": "OUTFOX_SEARCH_USER_ID=550783397@10.168.8.63; OUTFOX_SEARCH_USER_ID_NCOO=1648046863.5938306; JSESSIONID=aaau3c04aBOHla-FvCNDw; ___rl__test__cookies=1543629360108",
"Referer": "http://fanyi.youdao.com/",
}
data=parse.urlencode(self.data).encode("utf-8")
self.request=ub.Request(self.url,headers=self.headers,data=data)
return ub.urlopen(self.request)
def saveData(self,response):
"""
保存爬去到的数据
:param response: 获取到的数据
:return:
"""
print(response.read().decode("utf-8"))
if __name__ == '__main__':
translateWord=input("请输入你要翻译的英文单词:")
spider=youdaoSpider(translateWord=translateWord)
response=spider.sendPost()
spider.saveData(response) |