一、使用python自带模块urllib
- 模拟页面请求服务端,python提供了一个urllib模块,作用是通过python代码调用接口进行参数传递并获取到接口的返回值信息
- urllib模式是一个标准模块,直接import urllib即可
1、发送get请求
from urllib import request
from urllib import parse
import json
#get请求
# url='http://www.nzhp.cn/api/user/stu_info'
#
# data={'stu_name':'xiaohei'} #传递参数
#
# tmpData=parse.urlencode(data) #将数据变成kv形式,即k=v
# #接口+参数
# tmpUrl=url+'?'+tmpData # 将接口和参数拼接
# res=request.urlopen(tmpUrl) #请求接口
# resForRead=res.read() #通过read方法获取返回值结果,返回值结果是二进制bytes的类型
# resForString=resForRead.decode() #通过decode将bytes 转换成str类型,反过来是用encode
# resForDict=json.loads(resForString) #通过json将字符串转换成字典
# print(resForString)
# print(resForDict)
2、发送post请求
#post请求
url='http://www.nzhp.cn/api/user/login'
data={'username':'nhyy','passwd':'aA123456'}
tmpData=parse.urlencode(data) #变成k=v形式
#get和post的区别在于urlopen时,get发的是接口和参数拼成的字符串,post发送的是接口和参数,参数要求是bytes类型
res=request.urlopen(url,tmpData.encode()) #post请求参数1 为接口地址,参数2 为参数要求是bytes类型,使用encode将str转换成bytes类型
print(res.read().decode())
二者区别:
get和post的区别在于urlopen时,get发的是接口和参数拼成的字符串,post发送的是接口和参数,参数要求是bytes类型
二、第三方模块requests模块
由于urllib模块使用起来比较麻烦,这里介绍一个使用比较方便的requests模块。
使用时需要安装:pip install requests即可
import requests
#-------get请求----------
# url='http://www.nzhp.cn/api/user/stu_info'
# data={'stu_name':'xiaohei'} #传递参数
#
# res=requests.get(url,data).text #text方法返回的是字符串格式的返回值
# res=requests.get(url,data).json() #json()方法返回的是字典格式的返回值
# print(res)
# print(type(res))
#---------post请求--------------
# url='http://www.nzhp.cn/api/user/login'
# data={'username':'niu','passwd':'aA123456'}
#
# # res=requests.post(url,data).text text方法返回的是字符串格式的返回值
# res=requests.post(url,data).json() #json()方法返回的是字典格式的返回值
# print(res)
# print(type(res))
#post请求----------入参要求是json类型,可以通过在post中用-----------
# url='http://www.nzhp.cn/api/user/add_stu'
# data = {"name":"dsx123","grade":"一班","phone":11099999991}
#
# res=requests.post(url,json=data).json() #接口要求入参是json类型,可以通过在post请求中指定json
# print(res)
#post请求 ---------传递的参数中带cookie的------------
# cookie={'niuang':'0d11299888c03b25c9f89bb1231de23d'}
# url='http://www.nzhp.cn/api/user/gold_add'
# data={"stu_id":2,"gold":100}
# res=requests.post(url,data,cookies=cookie).text #通过cookies进行cookie的传递
# print(res)
#get请求 -------需要传递header------------
# url='http://www.nzhp.cn/api/user/all_stu'
# header={"Referer":"http://www.nzhp.cn/"}
# res=requests.get(url,headers=header).text
# print(res)
#post请求------传文件到服务器上---------
url='http://www.nzhp.cn/api/file/file_upload'
#通过files参数将文件传递到服务器上
# res=requests.post(url,files={'file':'open(urllib.test.py,rb)'}).text
res=requests.post(url,files={'file':open('urllib.test.py','rb')}).text #file是接口文档里传递的参数名
print(res)
三、jsonpath模块
需要安装 pip install jsonpath
当遇到层级比较深的字典时,要想取值使用jsonpath取值比较方便,可以一下取到。
如果取得值不存在,则返回False,
import jsonpath
d = {
"error_code": 0,
"stu_info": [
{
"id": 314,
"name": "矿泉水",
"sex": "男",
"age": 18,
"addr": "北京市昌平区",
"grade": "摩羯座",
"phone": "18317155663",
"gold": 100,
"cars": [
{"car1": "bmw"},
{"car2": "ben-z"},
]
}
]
}
car2 = jsonpath.jsonpath(d, '$..car2') #jsonpath是通过$..来定位
print(car2)