Python学习笔记(24)网络编程requests模块、jsonpath模块
一、requests模块
request模块是一个第三方模块,可用于HTTP接口测试
HTTP接口常用方法:get请求、post请求、发送header、发送cookie、发送json、下载文件、上传文件
GET请求:
import requests url = 'http://api.****.cn/api/user/stu_info' #get请求 req = requests.get(url,params={'stu_name':'abc'}) print(req.json()) #返回的json直接帮你转成了字典 print(req.text) #返回的就是字符串,如果返回的不是json的话,就要用它了 print(req.status_code) #返回的http状态码 print(req.content) #返回的bytes类型的,下载文件的时候用它
POST请求:
import requests #post请求 url="http://api.****.cn/api/user/login" req = requests.post(url,data={"username":"****","passwd":"aA123456"}) print(req.json())
发送cookie:
import requests #qq群管理 #使用浏览器开发者工具获得接口
#QQ群管理地址:https://qun.qq.com/member.html # url = 'https://qun.qq.com/cgi-bin/qun_mgr/get_group_list' # data = {"bkn": 1987868296} #传cookie两种方式:1、使用cookies={}来传 #将字符串类型的cookie转换为字典 #1.使用cookies={}来传,cookies中必须传字典类型 # s = '_qpsvr_localtk=0.9012023292190232; pgv_pvi=4462138368; pgv_si=s1034338304; uin=o1345741814; skey=@MuFHVWyJC; RK=jloomAjTbY; ptcz=96f512726408f5c2cb47965484725b46e5a4c753b1b2bbcbe8821134be7d3764; p_uin=o1345741814; pt4_token=34aj2ZvqVlX40QMhCU2XqMk6zJsv3nQtbVaR3Xeg2Yw_; p_skey=UcydxmonceN8i3FE7PGSbckH1CZ0kpjvRU8x2mqrmKo_; traceid=3664fbb14c' # d = {}#将字符串转换为字典 # for c in s.split(';'): # k,v = c.strip().split('=') # d[k] = v # req = requests.post(url,data,cookies=d) # print(req.json())
发送header(以cookie为例):
import requests #使用header对QQ群管理传递cookie header = {'cookie':"pgv_pvi=6636933120; RK=gRZhhBpNbS; ptcz=14bab564718e3e1048a09cc0e18a23f7c51f20d5b93da610cc1427f51f63a2f8; pgv_pvid=4990195883; ts_uid=5190463916; uin=o0511402865; pgv_si=s7505852416; skey=@2ttDS8Ljw; p_uin=o0511402865; pt4_token=AgqIsYBhO1b82zx1N4SxoGpCxGV0d38ss7jCI1nYfIg_; p_skey=9nlMjw4Uy44*Hu5iL3DOFonmAtZtExiniLykrsIRKmM_; traceid=14035c8a79"} req = requests.post(url,data,headers=header)#headers中传的header的字典key必须为'cookie' print(req.json())
发送json:
# url="https://oapi.dingtalk.com/robot/send?access_token=44402c9408df8cf3f429c02a20399fc34604f98cf572fcae26176a7" # today = datetime.datetime.now() # d = {"msgtype": "text","text": {"content": "现在是%s,大家不要忘记写作业哦!暗号:besttest"%today} } # req = requests.post(url,json=d) # print(req.json()) # print(req.cookies) #获取cookies
下载文件:
import requests #下载QQ头像,地址为:"https://q4.qlogo.cn/g?b=qq&nk=1345741814&s=140",nk为QQ号 url = "https://q4.qlogo.cn/g?b=qq&nk=1345741814&s=140"#获取QQ用户头像接口 req = requests.get(url) f = open('wjl.jpg','wb')#打开文件,wb为写字节格式 f.write(req.content)#content获取所有的字节数据 f.close()
上传文件:
import requests #上传文件 url = "http://api.****.cn/api/file/file_upload" f = open('sxy.mp4','rb') #1、文件上传content-type为json格式时 #data = {'file':f} #2、文件上传格式为form-data时 data = {'file':(filename,f,''image/jpeg'')} r = requests.post(url,files=data) print(r.json()) f.close()
二、jsonpath模块
背景:由于接口返回的json中的数据可能会被包裹很多层,一层层的取值比较麻烦,不停的get(key),此时引入jsonpath模块,使得找key更简单
安装jsonpath模块,pip install jsonpath
import jsonpath d = { "error_code": 0, "stu_info": [ { "id": 1779, "name": "abc", "sex": "男", "age": 18, "addr": "北京市昌平区", "grade": "1", "phone": "12355456643", "gold": 100 }, { "id": 1788, "name": "abc", "sex": "男", "age": 18, "addr": "北京市昌平区", "grade": "2", "phone": "12355456663", "gold": 100 }, { "id": 1811, "name": "abc", "sex": "男", "age": 18, "addr": "北京市昌平区", "grade": "2", "phone": "24214124144", "gold": 100 }, { "id": 1831, "name": "abc", "sex": "男", "age": 18, "addr": "北京市昌平区", "grade": "2", "phone": "13849504275", "gold": 100 } ] } result = jsonpath.jsonpath(d,'$..gold')#第一个入参传入需要解析的json,第二入参需要使用$.. 拼接key,结果返回一个列表 print(result)
返回结果:
[100, 100, 100, 100]