本文最后更新于 258 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com
requests 模块:HTTP for Human
import requests
Python 标准库中的 urllib2
模块提供了你所需要的大多数 HTTP
功能,但是它的 API
不是特别方便使用。
requests
模块号称 HTTP for Human
,它可以这样使用:
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
传入这些参数:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
查看 url
:
print(r.url)
http://httpbin.org/get?key2=value2&key1=value1
读取响应内容
Requests
会自动解码来自服务器的内容。大多数 unicode
字符集都能被无缝地解码。
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"}
查看文字编码:
r.encoding
'utf-8'
每次改变文字编码,text
的内容也随之变化:
r.encoding = "ISO-8859-1"
r.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
数据:
r.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
就会抛出一个异常。
响应状态码
r = requests.get('http://httpbin.org/get')
r.status_code
407
响应头
r.headers['Content-Type']
'text/html'