一、requests模块的基本方法
import requests
1.res = requests.get(url,params={‘stu_name‘:‘小黑‘}) #发送get请求,传参k-v形式
2.data={‘user‘:‘useraccount‘,‘pwf‘:‘password‘}
res = requests.post(url,data=data) #发送post请求,传参k-v形式
3.data={"name":"requests_name","grade":"天蝎座"}
res = requests.post(url,json=data) #发送post请求,入参是json类型的
4.cookie = {‘zhangsan‘:‘abd9a0995f4696e1a60133220b32037a‘}
res = requests.post(url,data=data,cookies=cookie) #发送post请求,带cookie的
5.header = {‘Referer‘:‘http://api.nnzhp.cn/‘}
res=requests.get(url,headers=header) #发送get请求,带header的
6.res = requests.get(url,verify=False) #发送https请求,需加上verify=False
7.res.json() #把返回的结果转成字典,必须返回是json
8.res.text #返回成字符串
9.res.content #用来下载文件用的,返回的是二进制
10.res.headers #获取到返回的所有header
11.res.cookies #获取到返回的所有cookie
12.res.status_code #获取到请求响应的状态码
二、例子
例子一(下载图片):
url6=‘https://aliimg.changba.com/cache/photo/855e5493-f018-44db-8892-c8660649327b_640_640.jpg‘res = requests.get(url6,verify=False) #verify=False如果是https的话加上这个with open(‘tu.jpg‘,‘wb‘) as fw: #打开一个文件 fw.write(res.content) #将请求获取到的结果写入到文件里去,是文件,所以是res.content二进制
例子二(获取qq群的成员信息存入到mongodb里以及下载成员的头像保存到本地):
import pymongo,os client = pymongo.MongoClient(host=‘x.x.x.x‘,port=xxxx) db=client[‘qq_group‘] table = db[‘xxx‘] url = "https://qun.qq.com/cgi-bin/qun_mgr/search_group_members" data = {"gc":xxxxx,"st":0,"end":200,"sort":0,"bkn":xxxxxxx} header = {"Cookie":"pgv_pvi=1977809920; tvfe_boss_uuid=d8ce464d0b7bd6f6; RK=6RFHD8oPOz; ptcz=11c139fb4d425800954a94bcb78d8b04222ef05985e6b9ac493a28744decc178; pgv_pvid=744932512; ptui_loginuin=2851662269; o_cookie=2851662269; pt2gguin=o0469873580; uin=o0469873580; ptisp=ctc; pgv_si=s8086261760; [email protected]; p_uin=o0469873580; pt4_token=sf7OadX0vT5Mv4PiIQ3xkHbiI9quA5GB75z8lMcLgag_; p_skey=PcX00u1AVFRWKu6N1S62ooT13P*JDcCOhq64nknIlGQ_"} res = requests.post(url=url,data=data,headers=header) mems = res.json().get("mems")#获取到该群的所有成员的信息 for i in mems: mem = { "qq":i.get("uin"), "gender":i.get("g"), "nick":i.get("nick"), "card":i.get("card"), "qage":i.get("qage") } if mem.get("gender") == 0: mem[‘gender‘] = "男" elif mem.get("gender") == 1: mem[‘gender‘] = "女" else: mem[‘gender‘] = "未知" if mem.get("card") == "": mem[‘card‘] = "没有群名片" table.insert_one(mem) qq = mem.get("qq") url= "http://q4.qlogo.cn/g?b=qq&nk={}&s=140".format(qq) pics = requests.get(url) abs_path=os.path.join(‘pics‘,str(qq)+‘.jpg‘) #拼接路径,都把图片保存到一个文件夹下面 with open(abs_path,"wb") as f: f.write(pics.content) print(‘%s的头像已经下载完成.‘%mem.get("nick"))
原文地址:https://www.cnblogs.com/yz-test/p/9387970.html