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)
    

      

  • 相关阅读:
    算法学习概述(2016.6)
    java异常和错误类总结(2016.5)
    java string 细节原理分析(2016.5)
    MySQL 5.7.18 解压版安装
    Struts2的<s:date>标签使用详解[转]
    jprofile查看hprof文件[转]
    iBatis的Settings节点参数详解[转]
    window.open、window.showModalDialog和window.showModelessDialog 的区别[转]
    oracle 字典表查询
    oracle 表空间操作
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9310763.html
Copyright © 2011-2022 走看看