一、urllib模块

urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模块,在python2里面有urllib模块和urllib2模块。

urllib模块太麻烦了,传参数的话,都得是bytes类型,返回数据也是bytes类型,还得解码,想直接把返回结果拿出来使用的话,还得用json,发get请求和post请求,也不通,使用比较麻烦

1 importjson2 from urllib importrequest3 from urllib importparse4
5 #【get请求】
6 url = ‘http://api.nnzhp.cn/api/user/stu_info‘
7
8 data={"stu_name":"xiaohei"}9
10 tmpData=parse.urlencode(data) #1、将数据变为k=v模式
11 print(tmpData)12 #接口+参数
13 tmpUrl=url+‘?‘+tmpData #接口参数拼接
14 print(tmpUrl)15 res = request.urlopen(tmpUrl) #请求接口
16 resForRead = res.read() #通过read安啊获取返回值结果,返回值结果为Bytes类型
17 print(res.read())18 #待b的是bytes类型 bytes类型转成str类型:后面加.decode()
19
20 resForString = resForRead.decode() #通过decode将bytes转成str类型
21 print(resForString)22
23 #2、想得到参数里面某一个字段,要先通过json变成字典的形式,然后再取值
24 resForDict = json.loads(resForString) #通过json将字典转成字典
25 print(resForDict)26
27 #3、必须符合字典的格式才能取值
28 #加上[]
29
30 #【POST】请求
31 url = ‘http://api.nnzhp.cn/api/user/login‘
32 data={"username":"niuhanyang","passwd":"aA123456"}33 tmpData = parse.urlencode(data) #k=v
34
35 #post 请求写法
36 res=request.urlopen(url,tmpData.encode()) #post请求 参数1为接口地址;参数2为bytes
37 print(res.read().decode())38 #post和get请求区别在于 urlopen时,get发的是接口和参数的平层字符串
{"error_code": 0,"login_info": {"login_time": "20191106204328","sign": "65ea3b950abe9aa55f23092449e1da3a","userId": 2170}
}
二、requests模块
需pip install requests导入
Requests 基于 urllib ,采用Apache2 Licensed 开源协议的 HTTP 库。它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求。 目前很多Python 爬虫也使用Requests 库
功能特性
Keep-Alive & 连接池
国际化域名和 URL
带持久 Cookie的回话
浏览器式的 SSL 认证
自动内容解码
基本/摘要式的身份认证
优雅的 key/value Cookie
自动解压
Unicode 响应体
HTTP(s)代理支持
文件分块上传
流下载
连接超时
分块请求
支持 .netrc (用户配置脚本文件)
requests.get() get请求
requests.post() post请求
requests.delete() delete请求
requests.put() put请求
【get请求】
1 importrequests2
3 #【get】请求
4 url = ‘http://api.nnzhp.cn/api/user/stu_info‘
5 data={"stu_name":"xiaohei"}6
7 res =requests.get(url,data).text8 print(res) #返回的是字符串格式的结果
9
10 #1、如果需要返回的是字典格式 后面加.json
11 res =requests.get(url,data).json()12 print(res)
【POST】请求
1 #【Post】请求
2 url = ‘http://api.nnzhp.cn/api/user/login‘
3 data={"username":"niuhanyang","passwd":"aA123456"}4
5 #res = requests.post(url,data).text
6 res =requests.post(url,data).json()7 print(res)
【入参传json】
1 #【入参是json】
2 url = ‘http://api.nnzhp.cn/api/user/add_stu‘
3 data = {"name":"dxw999","grade":"二年级","phone":"18217053374"}4
5 res = requests.post(url,json=data).json()6 print(res) #接口要求入参是json类型,可以通过在post请求中指定json
{‘error_code‘: 0, ‘msg‘: ‘操作成功!‘}
【充值金币接口】
1 #【充值金币接口】
2 #1、先登录 获取sign
3 url = ‘http://api.nnzhp.cn/api/user/login‘
4 data={"username":"niuhanyang","passwd":"aA123456"}5
6 #res = requests.post(url,data).text
7 res =requests.post(url,data).json()8 print(res)9
10 #2、生成cookie
11 cookie = {"niuhanyang":"160eb8812a08731ca9ce9c1ab6c6bc0f"}12 url = ‘http://api.nnzhp.cn/api/user/gold_add‘
13 data = {"stu_id":"1","gold":"10000"}14 res = requests.post(url,data,cookies=cookie).text #通过cookies传递cookie
15 print(res)
{"error_code": 0,"msg": "操作成功!"}
【获取header】
1 url = ‘http://api.nnzhp.cn/api/user/all_stu‘
2 header = {"Referer":"http://api.nnzhp.cn/"}3 res = requests.get(url,headers=header).text4 print(res)
【传入文件】
1 url = ‘http://api.nnzhp.cn/api/file/file_upload‘
2 #通过files参数将文件传递到服务器上
3 res = requests.post(url,files={"file":"open(‘urllib_test.py‘)"}).text4 print(res)
{"error_code": 0,"msg": "操作成功!"}