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的环境. 另外我想说的是,文中用到的下载链接尽量官方网站上的下载链接,这是我比 ...
随机推荐
- SQLServer------如何快速插入几万条测试数据
方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...
- 导入google地图
一直报地图页面的 java.lang.incompatibleclasschangeerror 想来想去,应该是包不兼容的原因,原本以为,在 build.gradle 里面 compileSdkVer ...
- nil、Nil、NULL与NSNull的区别及应用
总结 nil:OC中的对象的空指针 Nil:OC中类的空指针 NULL:C类型的空指针 NSNull:数值类的空对象 详细解析应用如下: 1.nil 指向一个对象的指针为空 在objc.h中的定义 ...
- linux配置免密登录
例如: $ ssh -i ~/ec2.pem ubuntu@12.34.56.78 首先确定你可以以密码的形式连接远程服务器,也可以创建一个非超级管理员用户,并增加 sudo 权限. $ sudo s ...
- mount: block device /dev/cdrom is write-protected, mounting read-only 解决方法
[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ mount: block device /dev/sr0 is write-protected, mo ...
- nested exception is org.springframework.beans.factory.BeanCreationException: 不能注入对象 创建对象失败 spring
[出现错误的背景] 在使用Spring+SpringMVC+Mybatis SSM集成框架时,服务器启动就会报错. [错误根源] XML配置错误. [解决方案] 第一步.查找springmvc.xml ...
- vux 局部注册组件
在home.vue里面,引入Prop.vue组件: 其中 <child :message="msg"></child>的时候 是这么赋值的: data () ...
- html5实现的一些效果
一.网页换肤 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- iconfont阿里爸爸做的开源图库
iconfont 三种使用姿势 1.unicode格式 优点 兼容性最好,支持ie6+ 支持按字体的方式去动态调整图标大小,颜色等等 缺点 不支持多色图标 在不同的设备浏览器字体的渲染会略有差别,在不 ...
- mac必装工具以及mac使用介绍
必装工具 Scroll Reverserhttp://pilotmoon.com/scrollreverser/:一款可以使得鼠标使用方式和windows系统一致的软件 编程工具 ,,,,, 常用快捷 ...