mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-09 05:15:28 +08:00
150 lines
2.9 KiB
Markdown
150 lines
2.9 KiB
Markdown
# requests 模块:HTTP for Human
|
||
|
||
In [1]:
|
||
|
||
```py
|
||
import requests
|
||
|
||
```
|
||
|
||
Python 标准库中的 `urllib2` 模块提供了你所需要的大多数 `HTTP` 功能,但是它的 `API` 不是特别方便使用。
|
||
|
||
`requests` 模块号称 `HTTP for Human`,它可以这样使用:
|
||
|
||
In [2]:
|
||
|
||
```py
|
||
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]:
|
||
|
||
```py
|
||
payload = {'key1': 'value1', 'key2': 'value2'}
|
||
r = requests.get("http://httpbin.org/get", params=payload)
|
||
|
||
```
|
||
|
||
查看 `url` :
|
||
|
||
In [4]:
|
||
|
||
```py
|
||
print(r.url)
|
||
|
||
```
|
||
|
||
```py
|
||
http://httpbin.org/get?key2=value2&key1=value1
|
||
|
||
```
|
||
|
||
## 读取响应内容
|
||
|
||
`Requests` 会自动解码来自服务器的内容。大多数 `unicode` 字符集都能被无缝地解码。
|
||
|
||
In [5]:
|
||
|
||
```py
|
||
r = requests.get('https://github.com/timeline.json')
|
||
|
||
print r.text
|
||
|
||
```
|
||
|
||
```py
|
||
{"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]:
|
||
|
||
```py
|
||
r.encoding
|
||
|
||
```
|
||
|
||
Out[6]:
|
||
|
||
```py
|
||
'utf-8'
|
||
```
|
||
|
||
每次改变文字编码,`text` 的内容也随之变化:
|
||
|
||
In [7]:
|
||
|
||
```py
|
||
r.encoding = "ISO-8859-1"
|
||
|
||
r.text
|
||
|
||
```
|
||
|
||
Out[7]:
|
||
|
||
```py
|
||
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]:
|
||
|
||
```py
|
||
r.json()
|
||
|
||
```
|
||
|
||
Out[8]:
|
||
|
||
```py
|
||
{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]:
|
||
|
||
```py
|
||
r = requests.get('http://httpbin.org/get')
|
||
|
||
r.status_code
|
||
|
||
```
|
||
|
||
Out[9]:
|
||
|
||
```py
|
||
407
|
||
```
|
||
|
||
## 响应头
|
||
|
||
In [10]:
|
||
|
||
```py
|
||
r.headers['Content-Type']
|
||
|
||
```
|
||
|
||
Out[10]:
|
||
|
||
```py
|
||
'text/html'
|
||
``` |