python3 requests 进行接口测试、爬虫使用总结
Requests 是第三方模块,如果要使用的话需要导入。Requests也可以说是urllib模块的升级版,使用上更方便。
这是使用urllib的例子。
import urllib.request
import json
url = 'http://www.weather.com.cn/data/sk/101190408.html'
res = urllib.request.urlopen(url)#发送请求
result = res.read().decode()#获取结果,结果是byte类型的需要decode()
print(json.loads(result))
下面是Requests 模块的使用。
支持的请求:
requests.get(‘https://github.com/timeline.json’) #GET请求
requests.post(“http:xxx.xx.com/post”) #POST请求
requests.put(“http:xxx.xx.com/put”) #PUT请求
requests.delete(“http:xxx.xx.com/delete”) #DELETE请求
requests.head(“http:xxx.xx.com/head”) #HEAD请求
requests.options(“http:xxx.xx.com/get”) #OPTIONS请求
发送GET请求:
import requests,json
url = 'http://api.xx.cn/api/user/stu_info?stu_name=hi'
req = requests.get(url)#发送get请求
print(req.text)#获取结果直接返回的就是json串
print(type(req.text)) #str
print(json.loads(req.text))#json转字典
print(req.json())#获取结果就是字典,只有返回的是json串的话才能用req.json()
print(type(req.json()))#dict
发送POST请求
url = 'http://api.xxx.cn/api/user/login'
data = {'username':'aa','passwd':''}
req = requests.post(url,data)#发送post请求,第一个参数是url,第二个参数是请求的数据
print(req.json())
发送格式为json的数据
url = 'http://api.xxx.cn/api/user/add_stu'
data = {
"name":"aa",
"grade":"bb",
"phone":13512530000, }
req = requests.post(url,json=data)#发送post请求,第一个参数是url,第二个参数是请求的数据,发送的json的话就写json=data
print(req.json())
发送带cookie的请求
url = 'http://api.xx.cn/api/user/gold_add'
data = {'stu_id':231,'gold':''}
cookie = {'aa':'3d867b361afdaac1381b02ae746c7278'}#key 为登陆的用户名,value为sign的值
req = requests.post(url,data,cookies=cookie)#添加cookie
print(req.json())
发送的请求中带Header
url = 'http://api.xxx.cn/api/user/all_stu'
header = {'User-Agent':'Chrome'}
req = requests.get(url,headers = header)
print(req.json())
上传文件
url = 'http://api.xxx.cn/api/file/file_upload'
f = open(r'D:\aa.jpg','rb')#图片要指定以二进制方式打开
r =requests.post(url,files={'file':f})
print(r.json())
下载文件,图片,视频
url = 'https://images2017.cnblogs.com/blog/412654/201712/412654-20171213115213238-464712233.png'
r =requests.get(url)
print(r.status_code)#获取请求状态码
print(r.content)#获取返回结果二进制格式的
fw = open('bt.jpg','wb')#当前路径
#fw = open(r'd:\bt.jpg','wb')#指定绝对路径
fw.write(r.content)#将二进制格式内容写入文件
fw.close()
爬虫,把网页保存到本地
url = 'http://www.cnblogs.com/nancyzhu/p/8029994.html'
r = requests.get(url)
f = open('page.html','wb')
f.write(r.content)
f.close()
python3 requests 进行接口测试、爬虫使用总结的更多相关文章
- Python3 + requests + unittest接口测试
一.缘 起 笔者最近完成了基于Python3 + requests + unittest的接口测试脚本,故在此做一下记录,于己为复盘,于彼为学习和参考 二.思 路 接口测试无非三步: 首先,造数据 - ...
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- python3+requests库框架设计01-自动化测试框架需要什么?
什么是自动化测试框架 关于自动化测试框架的定义有很多,在我大致理解下就是把能实现不同功能的软件组合在一起,实现特定的目的,这就是一个简单的自动化测试框架. 接口自动化测试框架核心无非是选择 一个用来编 ...
- python3+requests:接口自动化测试(二)
转载请注明出处:https://www.cnblogs.com/shapeL/p/9188495.html 前言:上篇文章python3+requests+unittest:接口自动化测试(一):ht ...
- Python3+Requests+Excel完整接口自动化框架
框架整体使用Python3+Requests+Excel:包含对实时token的获取 框架结构图 1.------base -------runmethond.py runmethond:对不同的请求 ...
- requests实现接口测试
python+requests实现接口测试 - get与post请求基本使用方法 http://www.cnblogs.com/nizhihong/p/6567928.html Requests ...
- python3+requests:使用类封装接口测试脚本
前言:接口测试用例较多,我们不可能每个用例都写一次requests,get或者requests,post等,所以对共用方法要进行封装处理 第一次修改:将get请求和post请求单独定义出来,使用过程中 ...
- 【python3】如何建立爬虫代理ip池
一.为什么需要建立爬虫代理ip池 在众多的网站防爬措施中,有一种是根据ip的访问频率进行限制的,在某段时间内,当某个ip的访问量达到一定的阀值时,该ip会被拉黑.在一段时间内被禁止访问. 这种时候,可 ...
- 2.1 Python3.5安装以及爬虫需要的环境配置
之所以选用Python,是因为对于网络爬虫来说,Python是最好上手的一种语言.本文讲述的安装配置都是基于Windows的环境. 另外我想说的是,文中用到的下载链接尽量官方网站上的下载链接,这是我比 ...
随机推荐
- sphinx的配置和管理.No2
网上配置文档众多,但是对着他们的文档来做老是出问题,于是花了点时间研究了一下,写成总结,方便以后查阅.也希望学习sphinx的朋友能少走弯路.Coreseek的安装请参考:http://blog.ch ...
- MyEclipse 2016 CI修改web项目context-root
右击项目properties——>搜索Deployment Assembly,修改如红框所示的Web Context Root
- hive 客户端执行select count(1) from t_sz01
yarn-site.xml配置错误 yarn.nodemanager.aux-services 中的-原先配置为_ 导致错误......
- 腾讯云大数据套件Hermes-MR索引插件使用总结
版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/121 来源:腾云阁 https://www.qclou ...
- pvm虚拟机基本原理
零.绪论:特别鸣谢下文博客,自己博客是对这篇博客的学习笔记: 大佬webber博客:https://www.cnblogs.com/webber1992/p/6597166.html 一.三种文件: ...
- Egret打包App 短暂黑屏解决方案 (Egret4.1.0)
论坛已经有人解决:http://bbs.egret.com/forum.php?mod=viewthread&tid=30288&highlight=app%2B%E9%BB%91%E ...
- iOS CoreMotion 纪录步数
- (void)startUpdateAccelerometer{ /* 设置采样的频率,单位是秒 */ NSTimeInterval updateInterval = 0.05; // ...
- Redis字符串类型的操作
set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , 9秒有效 注: 如果ex ...
- postgresql----Gist索引
GiST的意思是通用的搜索树(Generalized Search Tree). 它是一种平衡树结构的访问方法,在系统中作为一个基本模版,可以使用它实现任意索引模式.B-trees, R-trees和 ...
- MVC之Filter
过滤器的理解 Filter就是过滤器,在WebForm中,各种管道事件就是相当于过滤器,在MVC中,过滤器是单独的一种机制,分为方法过滤器和异常处理过滤器,方法过滤器实现的功能是在执行某一个请求得方法 ...