11.10 requests 模块:HTTP for Human
requests 模块:HTTP for Human
1import requests
Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 不是特别方便使用。
requests 模块号称 HTTP for Human,它可以这样使用:
1r = requests.get("http://httpbin.org/get")
2r = requests.post('http://httpbin.org/post', data = {'key':'value'})
3r = requests.put("http://httpbin.org/put")
4r = requests.delete("http://httpbin.org/delete")
5r = requests.head("http://httpbin.org/get")
6r = requests.options("http://httpbin.org/get")
传入 URL 参数
假如我们想访问 httpbin.org/get?key=val,我们可以使用 params 传入这些参数:
1payload = {'key1': 'value1', 'key2': 'value2'}
2r = requests.get("http://httpbin.org/get", params=payload)
查看 url :
1print(r.url)
http://httpbin.org/get?key2=value2&key1=value1
读取响应内容
Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码。
1r = requests.get('https://github.com/timeline.json')
2
3print r.text
{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t 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"}
查看文字编码:
1r.encoding
'utf-8'
每次改变文字编码,text 的内容也随之变化:
1r.encoding = "ISO-8859-1"
2
3r.text
u'{"message":"Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t 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 中也有一个内置的 JSON 解码器处理 JSON 数据:
1r.json()
{u'documentation_url': u'https://developer.github.com/v3/activity/events/#list-public-events',
u'message': u'Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t 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.'}
如果 JSON 解码失败, r.json 就会抛出一个异常。
响应状态码
1r = requests.get('http://httpbin.org/get')
2
3r.status_code
407
响应头
1r.headers['Content-Type']
'text/html'