Python爬虫之有道翻译

利用python的requests和hashlib库破解有道翻译加密,简单实现有道翻译。具体代码如下:

import hashlib
import random
import time
import requests

header = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    "Referer": "http://fanyi.youdao.com/",
    "Cookie": "OUTFOX_SEARCH_USER_ID=242784471@10.108.160.105; JSESSIONID=aaaob7_WSr5CC_bSwtAgx; OUTFOX_SEARCH_USER_ID_NCOO=1400180104.064204; ___rl__test__cookies=1587431771052"
}

url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
date = time.time()
date = int(date*1000)
e = input("输入要翻译的内容:")

ts = str(date)
salt = ts+str(random.randint(1,10))
sign =  hashlib.md5(("fanyideskweb"+e+salt+"Nw(nmmbP%A-r6U3EUn]Aj").encode('utf-8')).hexdigest()
bv = hashlib.md5("5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36".encode('utf-8')).hexdigest()

data = {
    'i': e,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': salt,
    'sign': sign,
    'ts': ts,
    'bv': bv,
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_CLICKBUTTION',
}

result = requests.post(url,headers= header,data=data).json()
r = result["translateResult"][0][0]["tgt"]
print("翻译结果:"+r)

执行结果:

图片[1]-Python爬虫之有道翻译--YYishare
图片[2]-Python爬虫之有道翻译--YYishare
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论