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'])
  • 相关阅读:
    iOS側拉栏抽屉效果Demo
    Juce源代码分析(九)应用程序基类ApplicationBase
    Android获取手机方向
    2014手机号码归属地数据库
    自译Solr in action中文版
    HUD 2031: 进制转换
    《学习opencv》笔记——矩阵和图像操作——cvConvertScale,cvConvertScaleAbs,cvCopy and cvCountNonZero
    Xcode5.1.1+ios 7.1.2 免证书真机调试
    《你不知道的JavaScript》读书笔记(二)词法作用域
    python生成word中文字体
  • 原文地址:https://www.cnblogs.com/Dark-fire-liehuo/p/9975976.html
Copyright © 2011-2022 走看看