码迷,mamicode.com
首页 > 编程语言 > 详细

10-python中的requests应用

时间:2018-07-14 20:04:03      阅读:4169      评论:0      收藏:0      [点我收藏+]

标签:import   coding   user   sts   com   5.0   client   script   ali   

使用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)

  

10-python中的requests应用

标签:import   coding   user   sts   com   5.0   client   script   ali   

原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9310763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!