zoukankan      html  css  js  c++  java
  • 利用爬虫技术,仿有道翻译小案例

    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": "OUTFOX_SEARCH_USER_ID=-1128661289@58.62.30.71; 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'])
  • 相关阅读:
    webstorm和git安装后,terminal输入git命令,提示'git' 不是内部或外部命令
    Vue中beforeRouterEnter和beforeRouteLeave的应用
    对象数组,对比多个数组的相同子元素并筛选
    应用canvas绘制动态时钟--每秒自动动态更新时间
    opacity 与rgba区别
    js对象的合并
    小程序wx.navigateTo和wx.redirectTo 都无效
    点赞博客园
    英语学习方法
    JAVA编程思想——分析阅读
  • 原文地址:https://www.cnblogs.com/Dark-fire-liehuo/p/9975976.html
Copyright © 2011-2022 走看看