python request模块学习
安装:
pip install requests
使用:
import requests
HTTP请求:GET、POST、PUT、DELETE、HEAD、OPTIONS
1) get
res = requests.get("https://github.com/timeline.json")
2) post
res = requests.post("http://httpbin.org/post");
3) put
res = requests.put("http://httpbin.org/put");
4) delete
res = requests.delete("http://httpbin.org/delete");
5) head
res = requests.head("http://httpbin.org/get") ;
6) options
res = requests.options("http://httpbin.org/get")
为URL传递参数
requests模块使用params关键字参数,以一个字典的形式来提供参数。
>>> payload = {'key1':'value','key2':'value2'}
>>> res = requests.get("http://httpbin.org/get",params=payload)
>>> res.url
u'http://httpbin.org/get?key2=value2&key1=value'
查看响应内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.text
u'{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'
requests模块会自动解码来自服务器的内容,可以使用res.encoding来查看编码,在你需要的情况下,request也可以使用定制的编码,并使用codes模块进行注册,这样你就可以轻松的使用这个解码器的名称作为res.encoding的值
>>> res.encoding
'utf-8'
>>> res.encoding = "gbk2312"
以json格式获取响应的内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.json()
{u'documentation_url': u'https://developer.github.com/v3/activity/events/#list-public-events', u'message': u'Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.'}
原始的应该内容
>>> res = requests.get("http://github.org/timeline.json")
>>> res.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x0000000002F31550>
>>> res.raw.read(10)
''
定制请求头
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> headers = {'content-type': 'application/json'}
post请求参数是这样:
>>> payload = {'key1':'value1','key2':'value2'}
>>> url = "http://httpbin.org/post"
>>> res =requests.post(url,data=payload)
>>> res.text
u'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "23", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.10.0"\n }, \n "json": null, \n "origin": "218.240.129.20", \n "url": "http://httpbin.org/post"\n}\n'
>>> print res.text
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.10.0"
},
"json": null,
"origin": "218.240.129.20",
"url": "http://httpbin.org/post"
}
响应状态码及响应头:
>>> res = requests.get("http://httpbin.org/get")
>>> res.status_code
200
>>> res.status_code == requests.codes.ok
True
>>> res.headers
{'Content-Length': '', 'Server': 'nginx', 'Connection': 'keep-alive', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Sun, 22 May 2016 09:24:10 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}
>>> print res.headers
{'Content-Length': '', 'Server': 'nginx', 'Connection': 'keep-alive', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Sun, 22 May 2016 09:24:10 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}
>>> res.headers['Content-Type']
'application/json'
>>> res.headers.get('content-type')
'application/json'
>>> res.headers.get('Connection')
'keep-alive'
>>>
Cookies:
访问cookies
>>> url = 'http://example.com/some/cookie/setting/url'
>>> r = requests.get(url)
>>> r.cookies['example_cookie_name']
设置cookies
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'
最新文章
- Entity Framework 教程——DBContext
- oracle中lead和lag函数 (转载)
- HDU 1069 dp最长递增子序列
- F - To the Max
- WGZX:javaScript 学习心得--2
- html5重定义标签
- HtmlAgilityPack 总结(一)
- BZOJ 1264 基因匹配Match(LCS转化LIS)
- Hadoop(一)Centos7虚拟机网络配置
- Tkinter 导入安装包
- mysql6.5 操作日志
- 不带parent指针的successor求解
- javascript、JSP、JS有什么区别和联系
- VueJs(7)---计算属性和侦听器
- open-falcon详解
- Java实现对象的序列化
- Git免输入密码提交
- 【记录】cygwin下折腾个人配置环境
- 6. Activity life cycle
- [BZOJ4554][TJOI2016&;&;HEOI2016]游戏(匈牙利)
热门文章
- Nutch2.3分布执行过程中Mongodb中数据的变化
- Android App开之标注切图
- SqlServer之表变量和临时表
- EC读书笔记系列之6:条款11 在operator=中处理自我赋值
- jquery的$().each,$.each的区别与应用
- 不可综合的verilog语句分析
- 【Chromium中文文档】Profile架构(看看谷歌家的重构)
- spring mvc 使用jsr-303进行表单验证的方法介绍
- linux系统配置Apache虚拟主机实例
- 网易云课堂_C语言程序设计进阶_第四周:ACL图形库