Share What
I Am Using or Studying

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)

执行结果:

赞(0) 打赏
未经允许不得转载:YYishare » Python爬虫之有道翻译
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)

觉得文章有用就打赏一下作者,您的支持是站长的最大动力

支付宝扫一扫打赏

微信扫一扫打赏