requests 常见方法总结
请求设置:requests.get/post
(
url,
data={},
params={},
headers={},
timeout=0.01,
files={}
Session()
verity=False 关闭ssl验证
proxies={} 代理设置
auth=HTTPBasicAuth() base认证
auth=HTTPDigestAuth() digest认证
)
响应获取:response .status_code
url
cookies
text
content 用来保存图片,视频
json()
1.说明
data={} 用于get请求
params={} 用于post请求
2.session()用法示例
场景一:
response = requests.get('http://httpbin.org/cookies/set/name/tom')
print(response.text)
response2 = requests.get('http://httpbin.org/cookies')
print(response2.text)
结果:{"cookies": {"name": "tom"}} {"cookies": {}} 获取不到cookie
场景二:
obj = requests.session()
rec = obj.get('http://httpbin.org/cookies/set/name/tom')
print(rec.text) rec2 = obj.get('http://httpbin.org/cookies')
print(rec2.text)
结果:{"cookies": {"name": "tom"}} {"cookies": {"name": "tom"}} 获取到cookie
3.verity用法示例
response = requests.get('https://www.12306.cn',verify=False) 如果不关闭verity会报错
print(response.text.encode("utf8"))
4.代理设置用法
proxies = {'http' : '61.135.217.7:80'}
response = requests.get('http://httpbin.org/get', proxies=proxies)
print(response.text)
5.auth用法示例
import requests
from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth response = requests.get('http://httpbin.org/basic-auth/mother/home', auth=HTTPBasicAuth("mother","home"))
print(response.text)
response2 = requests.get('http://httpbin.org/digest-auth/auth/my_mother/home', auth=HTTPDigestAuth("my_mother","home"))
print(response2.text)
5.模拟上传图片
file = {'img':open('df.jpg','rb')}
response = requests.post('http://httpbin.org/post',files=file)
print(response.text)
6.字典形式的url拼接
from urllib.parse import urlencode
data = {'city':'北京'}
print("https://www.sojson.com/?%s" % urlencode(data))
7.例子:unittest + requests + BSTestRunner
BSTestRunner :形成测试报告的库
1.安装 BSTestRunner
地址:https://github.com/easonhan007/HTMLTestRunner
将 BSTestRunner.py 文件放入目录 D:\IT\python3\Lib 下
2.开始工程(用例+测试报告)

import unittest
import time
from BSTestRunner import BSTestRunner test_dir = './test_cases'
report_dir = './reports'
discover = unittest.defaultTestLoader.discover(test_dir , pattern="dan.py")
now = time.strftime('%Y-%m-%d %H_%M_%S')
report_name = report_dir+'/'+now+'test_report.html' with open(report_name,'wb') as f:
runner = BSTestRunner(stream=f,title='Weather API Test Report',description='china wether')
runner.run(discover)
requests 常见方法总结的更多相关文章
- Python爬虫突破封禁的6种常见方法
转 Python爬虫突破封禁的6种常见方法 2016年08月17日 22:36:59 阅读数:37936 在互联网上进行自动数据采集(抓取)这件事和互联网存在的时间差不多一样长.今天大众好像更倾向于用 ...
- C#图片处理常见方法性能比较
C#图片处理常见方法性能比较 来自:http://www.cnblogs.com/sndnnlfhvk/archive/2012/02/27/2370643.html 在.NET编程中,由于GDI ...
- window对象中的常见方法
<body><!-- window对象中的常见方法--><script type="text/javascript"> var timeid; ...
- python socket 常见方法及 简单服务/客户端
socket 常见方法: 补充说明:what is file descriptor? 文件描述符是什么? 参考(http://stackoverflow.com/questions/8191905/w ...
- VBS操作Excel常见方法
VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...
- UIPickerView常见属性、常见方法(包括代理方法和数据源方法)的一些说明
一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- AJAX跨域的常见方法
由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...
- Java中字符串的一些常见方法
1.Java中字符串的一些常见方法 /** * */ package com.you.model; /** * @author Administrator * @date 2014-02-24 */ ...
随机推荐
- sql调优《二》
1.数据库设计(是否复合范式,是否合理归档.分区.分表等) 2.硬件基础架构 (设备规格,硬件性能,负载均衡,容灾等) 3.ql语句的写法.索引和统计信息,事务和锁,应用程序访问代码(连接过多.频繁开 ...
- AARRR:互联网创业者一定要掌握的指标
创业公司如何做数据分析?网站分析工具里的指标千百种,到底要从哪些数据入手呢?除了流量跟转换率,还有哪些数据跟公司成长有关呢?或许可以从了解AARRR Metrics开始.AARRR Metrics是由 ...
- canvas 画图优化
http://www.cnblogs.com/rhcad/archive/2012/11/17/2774794.html
- Jmeter中模拟多用户执行多场景操作
1.其实一个用户组就是一个场景(Thread Group).可以在一个测试计划中进行多个场景的执行,在测试计划下加一个全局的User Defined Variables,在这个里面可以设置执行总数to ...
- CF1037E Trips (离线+图上构造)
题目大意:一共有n个人,每天早上会有两个人成为朋友,朋友关系不具有传递性,晚上,它们会组织旅游,如果一个人去旅游,那么他不少于$k$个朋友也要和他去旅游,求每天的最大旅游人数 一开始并没有想到反向建图 ...
- ubuntu 12.04下安装Qt出现cannot execute binary file的解决方案
最近在ubuntu 12.04下安装QT的过程中,遇到一个问题. ./qt-opensource-linux-x64-5.7.0.run出现了bash: ./qt-opensource-linux-x ...
- 安装SSH、配置SSH无密码登录 ssh localhost
集群.单节点模式都需要用到 SSH 登陆(类似于远程登陆,你可以登录某台 Linux 主机,并且在上面运行命令),Ubuntu 默认已安装了 SSH client,此外还需要安装 SSH server ...
- SpringMVC上传文件(图片)并保存到本地
SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...
- Git学习总结(8)——Git和SVN之间的基本区别
GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...
- CSVHelper读出乱码 解决方案
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamRe ...