requests库的安装 pip install requests
一/带payload参数的get请求
import requests #带参数请求,系统自动将字典形式的参数转换成url中可识别的参数 payload={'key1':'value1','key2':'value2'} r=requests.get("https://docs.python-requests.org/en/master/",params=payload) print(r.url)
get不安全因为参数都显示在网址栏
二/带data参数的post请求
这种方式参数不会直接显示在url上
#利用data参数post添加参数 data_form={'key1':'value1','key2':'value2'} r=requests.post("https://docs.python-requests.org/en/master/",data=data_form) print(r.url)
三/上传文件
files={'file':open('test.txt','rb')}
r=requests.post("https://docs.python-requests.org/en/master/",file=files)
-----------------
with open('test.txt') as f:
r=requests.post("https://docs.python-requests.org/en/master/",data=f)
四/获取发送cookies
#获取cookies req=requests.get("https://www.baidu.com") cks=req.cookies print(cks) for key in cks.keys(): print(key) print(cks[key])
#发送cookies cookies=dict(mycookie='test') req1=requests.get("https://www.baidu.com",cookies=cookies) print(req1.text)
五/超时配置timeout
#连接超时配置 req2=requests.get("http://127.0.0.1:5000",timeout=1)