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