get,post和带参数请求
import requests
# get请求
res = requests.get("http://httpbin.org/get")
print(res.status_code, res.reason)
print(res.text)
# 带参数的get请求
res = requests.get("http://httpbin.org/get", params={"a": 1, "b": "2"})
print(res.json())
# post请求
res = requests.post("http://httpbin.org/post", data={"a": 1})
print(res.json())
结果
自定义headers和带cookie请求
# 自定义headers请求
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
headers = {"User-Agent": ua}
res = requests.get("http://httpbin.org/headers", headers=headers)
print("自定义headers请求:\n", res.json())
# 带cookie的请求
cookies = dict(userid = "123456", token="xxxxxxxxxxxxxxxxx")
res = requests.get("http://httpbin.org/cookies", cookies=cookies)
print("带cookie的请求:\n", res.json())
输出
身份认证
res = requests.get("http://httpbin.org/basic-auth/admin/123456", auth=("admin", 123456))
print("Basic-auth认证请求:\n", res.json())
输出
主动抛出状态异常
bad_r = requests.get("http://httpbin.org/status/404")
print(bad_r.status_code)
bad_r.raise_for_status()
输出
session使用(带cookie)
# 使用requests.Session()对象请求
s = requests.Session()
# session对象会保存服务器返回的set-cookies头信息里面的内容
s.get("http://httpbin.org/cookies/set/userid/123456789")
# 下一次请求会自动将本地所有的cookies信息自定添加到头信息里面
r = s.get("http://httpbin.org/cookies")
print("检查session中的cookies", r.json())
输出
延迟
res = requests.get("http://httpbin.org/delay/4", timeout=5)
print(res.text)
输出