[包计划] requests
requests
Python 中一个易用的网络请求库
来源
使用
发送请求
r = requests.get('https://api.github.com/events')
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')
传递 URL 参数
# 字典里值为 None 的键都不会被添加到 URL 的查询字符串里
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
响应内容
import requests
r = requests.get('https://api.github.com/events')
print(r.text) # [{"id":"8462576403"}]
print(r.encoding) # utf-8
二进制响应内容
import requests
r = requests.get('https://api.github.com/events')
print(r.content)
JSON 响应内容
import requests
r = requests.get('https://api.github.com/events')
print(r.json()) # [{'id': '8462617752'}]
原始响应内容
import requests
r = requests.get('https://api.github.com/events', stream=True)
print(r.raw) # <urllib3.response.HTTPResponse object at 0x10c813550>
定制请求头
import requests
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
print(r) # <Response [404]>
复杂的 POST 请求
import requests
import json
# 表单
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
# JSON
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
print(r.text)
# JSON 2.4.2 版的新加功能
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, json=payload)
print(r.text)
POST一个多部分编码(Multipart-Encoded)的文件
import requests
url = 'http://httpbin.org/post'
files = {'file': open('test.txt', 'rb')}
r = requests.post(url, files=files)
print(r.text)
响应状态码
import requests
r = requests.get('http://httpbin.org/get')
print(r.status_code) # 200
print(requests.codes.ok) # 常量,代表 200
print(requests.codes.not_found) # 常量,代表 404
bad_r = requests.get('http://httpbin.org/status/404')
print(bad_r.status_code) # 404
bad_r.raise_for_status() # 错误请求 抛出异常
响应头
import requests
r = requests.get('http://httpbin.org/get')
print(r.headers)
print(r.headers['Content-Type']) # application/json
print(r.headers['content-type']) # application/json
Cookie
import requests
# 获取 cookie
url = 'http://httpbin.org/cookies'
r = requests.get(url)
print(r.cookies)
# 设置 cookie
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
print(r.text)
重定向与请求历史
import requests
# 追踪重定向
r = requests.get('http://github.com')
print(r.history) # [<Response [301]>]
# 禁用重定向
r = requests.get('http://github.com', allow_redirects=False)
print(r.status_code) # 301
超时
import requests
r = requests.get('http://github.com', timeout=0.001)
错误与异常
- 遇到网络问题时,
Requests
会抛出一个ConnectionError
异常。 - 如果
HTTP
请求返回了不成功的状态码,Response.raise_for_status()
会抛出一个HTTPError
异常。 - 若请求超时,则抛出一个
Timeout
异常。 - 若请求超过了设定的最大重定向次数,则会抛出一个
TooManyRedirects
异常。 - 所有
Requests
显式抛出的异常都继承自requests.exceptions.RequestException
。