zoukankan      html  css  js  c++  java
  • 10-python中的requests应用

    使用request方便:

    #_*_ coding: utf-8 _*_
    '''
    Created on 2018年7月14日
    
    @author: sss
    '''
    
    import requests
    import json
    
    #根据协议类型选择不同的代理
    proxies = {
            "http" : "118.190.95.43:9001",
            "https": "49.79.156.109:8000",
        }
    
    headers = {
            "Connection" : "keep-alive",
            "Accept" : "application/json, text/javascript, */*; q=0.01",
            "X-Requested-With" : "XMLHttpRequest",
            "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
            "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
            "Referer" : "http://fanyi.youdao.com/"
        }
    
    formdata = {
            "i" :'hello',
            "from " :"AUTO",
            "to" :"AUTO",
            "smartresult" :"dict",
            "client" :"fanyideskweb",
            "salt" :"1531403738742",  #这个应该是个时间戳
            "sign" :"ffa2b29fe52953208226d97a174bcea7", #应该是根据时间戳+你要翻译的内容加密后生成的验证字段
            "doctype" :"json",
            "version" :"2.1",
            "keyfrom" :"fanyi.web",
            "action" :"FY_BY_REALTIME",
            "typoResult" :"false"
        }
    
    # url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"  #http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule要把_o去掉
    
    response = requests.post(url, data = formdata, headers = headers, proxies = proxies)
    
    print(response.text.encode(encoding='utf_8') )
    
    #如果是json文件可以直接显示
    print(response.json())
    
    html = response.text
    target = json.loads(html)
    print("翻译结果:%s"%(target['translateResult'][0][0]['tgt'])) #读出结果
    
    #返回cookiejar对象:
    cookiejar = response.cookies
    
    #将cookieJar转为字典:
    cookiedict = requests.utils.dict_from_cookiejar(cookiejar)
    
    print(cookiejar)
    
    print(cookiedict)
    

      

  • 相关阅读:
    linux主机安装配置chrony时间同步器
    LINUX正则表达式
    文件系统
    “好文章”链接-爬虫脚本
    优秀博客集
    负载均衡LVS(Linux Virtual Server)
    LNMP
    MySQL备份还原
    iptables--SNAT、DNAT实践
    MySQL 基础命令
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9310763.html
Copyright © 2011-2022 走看看