10-python中的requests应用
Posted ystraw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
以上是关于10-python中的requests应用的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段