python爬虫有道词典翻译
程序员文章站
2022-05-04 11:50:02
...
有道词典的翻译应用(post请求方式)
from urllib import request
import time,random
from 爬虫.day01.interface_all import post
import json
# md5加密
def md5_object(st):
import hashlib
md5_obj = hashlib.md5()
md5_obj.update(st.encode('utf-8'))
# 加密后
result = md5_obj.hexdigest()
return result
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
def translate(word):
# i = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10)),
salt = str(int(time.time()*1000 + random.randint(0,10)))
# o = u.md5(S + n + r + D);
S = "fanyideskweb"
D = "ebSeFb%=XZ%T[KZ)c(sy!"
s = S + word + salt + D
sign = md5_object(s)
# 需要提交的参数
data = {
'i': word,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTIME',
'typoResult': 'false',
}
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
# 'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
# 'Content-Length': '214',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': '[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=1026585271.7822934; JSESSIONID=aaa8DVniSYPWYfQxzw2uw; ___rl__test__cookies=1534216136014',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
result = post(url,data,headers)
res = json.loads(result.decode('utf-8'))
return res['translateResult'][0][0]['tgt']
if __name__ == '__main__':
word = input('请输入要翻译的词语:')
result = translate(word)
print(result)
上一篇: Unity animator不剪辑动画实现 分段播放动画
下一篇: 自定义属性动画