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

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#uuGR@r"
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'])

猜你喜欢

转载自www.cnblogs.com/Dark-fire-liehuo/p/9975976.html