利用爬虫技术,仿有道翻译小案例

Posted L某人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用爬虫技术,仿有道翻译小案例相关的知识,希望对你有一定的参考价值。

import requests
import time
import hashlib
import json

inputInfo = input(请输入你想要翻译的内容:)
# 请求的url必须是点击翻译后跳转出来的页面路由
url = http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
# 观察form表单和header请求头每次访问会有哪些变量发生变换,此处form发生变化的有salt,sign,i(为查询得字符),
# 根据表单提交发现变动的js,寻找相应js里的salt,sign,值得设置和由来,并按相同得方法编辑其所需,
newtime = int(time.time() * 1000)
print(newtime)
e = inputInfo
sign = "fanyideskweb" + e + str(newtime) + "sr_3(QOHT)L2dx#[email protected]"
sign = hashlib.md5(sign.encode(utf-8)).hexdigest()
formData = {
    "i": e,
    "from": "AUTO",
    "to": "AUTO",
    "smartresult": "dict",
    "client": "fanyideskweb",
    "salt": newtime,
    "sign": sign,
    "doctype": "json",
    "version": "2.1",
    "keyfrom": "fanyi.web",
    "action": "FY_BY_CLICKBUTTION",
    "typoResult": "false",
}
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    # "Content-Length": "218",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    # 发生变化得是154开头的时间戳
    "Cookie": "[email protected]; JSESSIONID=aaayla8sm5ouFaBpGxBCw; OUTFOX_SEARCH_USER_ID_NCOO=702230926.9325526; ___rl__test__cookies={}".format(
        newtime),
    "Host": "fanyi.youdao.com",
    "Origin": "http://fanyi.youdao.com",
    "Pragma": "no-cache",
    "Referer": "http://fanyi.youdao.com/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
}
# 将表单和请求头一起提交,请求
response = requests.post(url, headers=headers, data=formData)
content = response.content.decode()
print(content)
# loads将字符串转成字典,load
res = json.loads(content)
print(res)
print(res[translateResult])
print(你的查询结果为:, res[translateResult][0][0][tgt])

 

以上是关于利用爬虫技术,仿有道翻译小案例的主要内容,如果未能解决你的问题,请参考以下文章

爬虫案例之网易有道翻译Python代码改写

网络爬虫技术创建属于自己的有道词典

Python3爬虫有道翻译

Python爬虫|有道翻译(简易版)

Python3网络爬虫:利用urllib.urlopen向有道翻译发送数据获得翻译结果

Python3网络爬虫:利用urllib.urlopen向有道翻译发送数据获得翻译结果