print(r.text)#字符串格式返回
print(r.json())#字典
print(r.content)#返回bytes类型
print(r.status_code)#返回状态码
什么使用用什么方法,
测试接口有没有通过,返回状态码
从返回的数据里面取值,获取某个字典的值需要用.json这是返回一个字典,将josn串转成字典
什么时候用字符串,有时候访问一个网站,获取网页信息,需要用text
什么时候用content,有时候你要下载图片,下载文件都是二进制的东西,所以需要这种方法
需要使用requrest模块
发cookie请求
import requests
url='https://qun.qq.com/cgi-bin/qun_mgr/search_group_members'
#传cookie方法,cookie比较少用第一种,比较多用headers这种方法,可以加多种方法headers里面
headers={"cookie":"pgv_pvi=8260104192; pgv_pvid=8735389392; tvfe_boss_uuid=48f126791da9cadd; RK=wN5MOyFDTz; ptcz=d3043d06d332b49d70da976b2039503867ddf097777972f14545144055ac0382; iip=0; o_cookie=964066767; pac_uid=1_964066767; mobileUV=1_174955bbe64_9c740; pgv_si=s9439393792; pgv_info=ssid=s1159621919; _qpsvr_localtk=0.5554568764618779; uin=o0964066767; p_uin=o0964066767; traceid=d9d1e2aeec; skey=@XQSFfMJGE; pt4_token=hEzAv*t2ponN5CkqBOirMdqaPIpBk5U2B3HSXxN5KLE_; p_skey=sk1YsR83oIYVz1BHaHAoEgMcUjcTTmQTxQpt19eBPmA_"}
data={"gc":"1078641913","st":"0","end":"20","sort":"0","bkn":"599353968"}
r=requests.post(url,data=data,headers=headers)#发送请求
print(r.text)#字符串格式返回
# print(r.json())#字典
# print(r.content)#返回bytes类型
# print(r.status_code)#返回状态码
发json请求
import requests
url='https://qun.qq.com/cgi-bin/qun_mgr/search_group_members'
#传cookie方法,cookie比较少用第一种,比较多用headers这种方法,可以加多种方法headers里面
headers={"cookie":"pgv_pvi=8260104192; pgv_pvid=8735389392; tvfe_boss_uuid=48f126791da9cadd; RK=wN5MOyFDTz; ptcz=d3043d06d332b49d70da976b2039503867ddf097777972f14545144055ac0382; iip=0; o_cookie=964066767; pac_uid=1_964066767; mobileUV=1_174955bbe64_9c740; pgv_si=s9439393792; pgv_info=ssid=s1159621919; _qpsvr_localtk=0.5554568764618779; uin=o0964066767; p_uin=o0964066767; traceid=d9d1e2aeec; skey=@XQSFfMJGE; pt4_token=hEzAv*t2ponN5CkqBOirMdqaPIpBk5U2B3HSXxN5KLE_; p_skey=sk1YsR83oIYVz1BHaHAoEgMcUjcTTmQTxQpt19eBPmA_"}
data={"gc":"1078641913","st":"0","end":"20","sort":"0","bkn":"599353968"}
r=requests.post(url,json=data,headers=headers)#发送请求json
print(r.text)#字符串格式返回
# print(r.json())#字典
# print(r.content)#返回bytes类型
# print(r.status_code)#返回状态码
传文件和下载文件的请求如何发呢
import requests
url='http://api.nnzhp.cn/api/file/file_upload'
date={"file":open('dds.py','rb')}
r=requests.post(url,files=date)#发送上传文件请求
print(r.text)#字符串格式返回
import requests
url="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608013894122&di=9775e4675d27c168324a0b294d767e82&imgtype=0&src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190114%2F23%2F1547478110-MSCTpnWoJk.jpg"
r=requests.get(url,)#发送请求
f=open('a.pg','wb')
f.write(r.content)#下载文件操作
f.close()
print(r.status_code)#状态码
如果请求的地址是https地址报错的请客下就加上 verify=False