用于发送http请求,可用于爬取数据,与API交互,测试web服务,向web服务器发送请求并获取响应结果。请求包括:get、post、put、delete、head、option等。
url = ' ' # 发送请求的地址
headers = {
' ' : ' '
} # 请求头
data = {
'key':'value'
} # 请求体 字典
在get中使用params=data,post中使用data=data
response1 = requests.get(url, headers=heades, params=data)
response2 = requests.post(url, headers=heades, data=data)
# Cookie参数两种用法
# 1、在请求头中使用
heades = {
'Content-Type': 'application / json;charset=UTF-8',
'Cookie':
'JSESSIONID=FB14D55791EA3EA44E3C9939D9B6BEFE4C8CE4BD3A7B22F3D76EB68406EB0F5A'
}
# 2、 赋予cookies 换成字典格式
cookies = {
'JSESSIONID':'FB14D55791EA3EA44E3C9939D9B6BEFE4C8CE4BD3A7B22F3D76EB68406EB0F5A'
}
response1 = requests.get(url, headers=heades,cookies=cookies)