首先需要导入模块
import requests
携带get请求方式
params = {
'xxx': 'yyy',
}
res = requests.get('xxx',params=params)
print(res.text) # 打印响应体内容
携带请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'
}
requests.get('xxx',headers=headers)
post请求携带data
data = {
'xxx': 'yyy'
}
requests.get('xxx',data=data)
# data参数:urlencoded
requests.post('地址',data=b'name=xxx&age=19')
# json编码:json
requests.post('地址',json={'name':'xxx','age':19})
携带cookie
cookie = {
'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqaWQiOiJjZHVfNTMyMDcwNzg0NjAiLCJleHBpcmUiOiIxNzEwOTAxOTY5NTM2In0.eseWTCMqp-yHa7rWgSvPhnWVhhQAgqGIvIgLGbvcBcc'
}
requests.post('xxx',cookies=cookie)