官方文档
https://2.python-requests.org/en/master/
安装
pip install requests
pip3 install requests
conda install requests
各种请求方式
1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|
get | post | put | delete | head | options |
请求
基本GET请求
import requests
data = {'name': '...', 'age': 22}
response = requests.get(url, params=data)
解析json
import requests, json
response = requests.get(url)
print(response.json())
print(json.loads(response.text))
获取二进制数据
import requests
url = '<https://github.com/favicon>'
response = requests.get(url)
# <class 'str'=""> <class 'bytes'="">
print(type(response.text), type(response.content))
print(response.text)
print(response.content)
# 下载图片或视频
with open(filename, 'wb') as f:
f.write(response.content)
# 添加headers
import requests
headers = {'User-Agent': '...'}
response = requests.get(url, headers=headers)
print(response.text)
基本POST请求
import requests
data = {'name':'...', 'age': 22}
headers = {'User_Agent': '...'}
response = requests.post(url, data=data, headers=headers)
响应
response属性
import requests
response = requests.get(url)
print(type(response.status_code), response.status_code) # <class 'int'="">
print(type(response.headers), response.headers)
#<class 'requests.strutures.caselnsensitivedict'="">
print(type(response.cookies), response.cookies)
# <class 'requests.cookies.requestscookiejar'="">
print(type(response.url), response.url) # <class 'str'="">
print(type(response.history), response.history) # <class 'list'="">
状态码判断
import requests
response = requests.get('//www.greatytc.com')
exit() if not response.status_code == 200 else print('Request Successfully')
高级操作
文件上传
import requests
files = {'file': open{filename, 'rb'}}
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)
获取cookie
import requests
response = requests.get(url)
print(response.cookies)
for k, v in response.cookies.items():
print(k + '=' + v)
会话维持
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
证书验证
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get(url, verify=False)
print(response.status_code)
import requests
response = requests.get(url, cert=('', ''))
print(response.status_code)
代理设置
import requests
proxies = {
'http': '...',
'https': '...',
}
response = requests.get(url, proxies=proxies)
print(response.status_code)
import requests
proxies = {
'http': 'http://user:password@127.0.0.1:5024/',
'https': '...',
}
response = requests.get(url, proxies=proxies)
print(response.status_code)
pip install 'requests[socks]'
import requests
proxies = {
'http': 'socks5://...',
'https': '...',
}
response = requests.get(url, proxies=proxies)
print(response.status_code)
超时设置
import requests
response = requests.get(url, timeout=1)
print(response.status_code)
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get(url, timeout=1)
print(response.status_code)
except ReadTimeout:
print('Timeout')
认证设置
import requests
from requests.auth import HTTPBasicAuth
r = requests.get(url, auth=HTTPBasicAuth('user': '123'))
pint(r.status_code)
import requests
r = requests.get(url, auth=('user': '123'))
print(r.status_code)
异常处理
import requests
from requests.exceptions import ReadTimeout, ConnectionError, RequestException
try:
response = requests.get(url, timeout=1)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except ConnectionError:
print('ConnectionError')
except RequestException:
print('Error')