基础使用get、post请求
常用get请求范例
import requests
sess = requests.Session()
headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/81.0.4044.122 Safari/537.36',
'Connection': 'keep-alive',
'Content-Type': 'application/json'}
sess.headers = headers
params = None
resp = sess.get('http://www.baidu.com', params=params, verify=False)
print(resp.text)
常用post请求范例
import requests
sess = requests.Session()
headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/81.0.4044.122 Safari/537.36',
'Connection': 'keep-alive',
'Content-Type': 'application/json'}
sess.headers = headers
data = None
jsondata = None
resp = sess.post('http://www.baidu.com', data=data, json=jsondata verify=False)
print(resp.text)
headers请求头
headers是以dict传递的,一般使用较多的是场景是headers中添加cookie调用api,如下:
headers = {'Accept': 'application/json, text/javascript, */*; q=0.01',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/81.0.4044.122 Safari/537.36',
'Connection': 'keep-alive',
# 'connection':'close',
'Content-Type': 'application/json',
'Cookie': 'rp=7f8d3933-aff3-42b8-b9ba-e0211716renderer.sid=s%3A76xyVefkHimvKH-}
获取请求头头部信息:r.request.headers
获取响应头部信息:r.headers
get参数params
当get请求连接需要带上参数,一般使用params,以字典的形式传入。
params= {'fieldType': 'CUSTOM', 'pageNo': 1, 'pageSize': 10}
r = sess.get('https://home.test', params=params, verify=False)
post参数data、json
当post请求需要带上html表单参数时,可用data参数,以字典的形式传入;
当post请求需要带上json格式参数时,可以json参数,可将字典形参数自动编码。
# 范例一:以web表单形式传入
jsondata = None
postdata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
resp = sess.post(url, verify=False, json=jsondata, data=postdata)
#范例二:以json格式形式传入
postdata = None
jsondata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
resp = sess.post(url, verify=False, json=jsondata, data=postdata)
范例三:通过json.dumps()编码成json再传入
jsondata = None
postdata = {'fieldName': 't_1', 'fieldKey': 'c_t1', 'dataType': 'TEXT',
'fieldDesc': 's', 'optiontalFalg': 0, 'optiontalItems': '', 'events': []}
resp = sess.post(url, verify=False, json=jsondata, data=json.dumps(postdata))
保持会话session
有时候我们希望在跨请求中保持同一个cookie,可用到Session();
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")
print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'
会话结合上下文管理器使用:
with requests.Session() as s:
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
response解析
重定向相关
可通过Response.history来追踪重定向;
allow_redirects参数可禁用重定向处理
r = requests.get('http://github.com')
r.history #[<Response [301]>]
# 禁用重定向
r = requests.get('http://github.com', allow_redirects=False)
r.status_code #301
r.history #[]
证书验证相关
verify=True
#Requests可忽略ssl证书的验证
其他
requests.packages.urllib3.disable_warnings()
#忽略警告:InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.