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 */ ...
随机推荐
- X Macro
30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...
- input输入框只允许输入数字
/* input输入框只允许输入数字*/ <input type="text" onkeypress="keyPress()" > function ...
- C/C++函数调用约定与this指针
关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的. VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调 ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- Laravel 5.5 项目安装报错
错误一. [Composer\Exception\NoSslException] The openssl extension is required for SSL/TLS protection bu ...
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- [terry笔记]一个在线美化sql的网站
http://www.dpriver.com/pp/sqlformat.htm 甚是好用.
- Pointcut is not well-formed: expecting 'name pattern' at character position 36
Pointcut is not well-formed: expecting 'name pattern' at character position 36 学习了:http://blog.csdn. ...
- 非典型的scala程序及其编译后的结果
初学Scala Folder structure以及部分代码 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2xldmVyd3lx/font/5a6L5L ...
- BZOJ 1057: [ZJOI2007]棋盘制作 悬线法求最大子矩阵+dp
1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑 ...