导入模块
import requests
get 请求
1.没有参数类型的
url='https://q4.qlogo.cn/g?b=qq&nk=23234&s=140'#获取qq图像的接口,nk就是qq号,不同的nk获取不同人的图像
r=requests.get(url)
with open('test.jpg','wb') as fw:#本地创建一个test.jpg的文件,wb以二进制形式写入
fw.write(r.content)#执行完成后本地会有一个test.jpg,打开之后就是qq号是2323这个人的图像
2.有参数类型的
url ,参数,cookie 瞎写的,具体参照接口文档啊~~~~
url='https://xxccxx'
arge={'hah':'test'}
r=requests.get(url,agre)
with open('test.txt','w') as fw:#(图片视频音乐就用wb二进制写)
fw.write(r)
post请求
1.有参数的
url='https://xxccxx'
arge={'hah':'test','hah2':'test1'}#有几个参数写几个
r=r.requests.post(url,arge)
print(r.json())
print(r.text)#比上面的形式好看一点点
2.有cookie的
url='https://xxccxx'
arge={'hah':'test','hah2':'test1'}#有几个参数写几个
cookie={'cookie':"quic=":443"; ma=2592000; v="46,44,43,39"}
r=requests.post(url,arge,headers=cookie)
print(r.text)
3.入参是json的
url='https://xxccxx'
arge={'hah':'test','hah2':'test1'}
r=requests.post(url,arge,json=arge)
print(r.text)
4.入参是file
url='https://xxccxx'
data = {'file':open('上周作业.py','rb')}
req = requests.post(url,files=data)
print(req.text)
-
接口请求失败,重试
import requests import retrying @retrying.retry(stop_max_attempt_number=3) def req(): url='https://xxccxx' arge={'hah':'test','hah2':'test1'} try: r=requests.post(url,arge,json=arge) except : print("接口请求失败,请重试")