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 */ ...
随机推荐
- JSON是什么?为JavaScript准备的数据格式
JSON是什么?为JavaScript准备的数据格式 还不了解JSON是什么?看了下面这篇文章,您对JSON是什么应该能够有了一个比较清晰的概念. JSON 即 JavaScript. Object ...
- Python爬虫:爬取糗事百科
网上看到的教程,但是是用正则表达式写的,并不能运行,后面我就用xpath改了,然后重新写了逻辑,并且使用了双线程,也算是原创了吧#!/usr/bin/python# -*- encoding:utf- ...
- DB2数据库load出现SQL3508N问题
SQL3508N装入或装入查询期间,当存取类型为 "<文件类型>"的文件或路径时出错.原因码:"<原因码>".路径:"< ...
- Mongodb 启动关闭脚本并设置开机自动启动Mongodb
配置文件内容:[root@yoon etc]# cat mongod.conf logpath=/export/log/mongodb.loglogappend=truefork = truedbpa ...
- WCF(二)配置文件
上篇文章中对WCF的配置放到App.config中,这样可以使程序更灵活.更具有扩展性. 下面说下配置文件中各个节点的含义. 服务端: WCF配置文件节点放在<system.serviceMod ...
- ivew Modal rule校验冲突问题
问题描述:新建和编辑公用的是一个弹框,打开新建关闭之后,会影响到编辑的弹框. 解决方法:赋值前先重置一下表单 this.$refs["你的表单"].resetFields();
- 使用剩余参数代替 arguments (prefer-rest-params)
使用剩余参数代替 arguments (prefer-rest-params) 剩余参数来自于ES2016.可以在可变函数中使用这个特性来替代arguments变量. arguments没有Array ...
- C语言实现面向对象(转)
1.引言 面向对象编程(OOP)并不是一种特定的语言或者工具,它只是一种设计方法.设计思想. 它表现出来的三个最基本的特性就是封装.继承与多态. 很多面向对象的编程语言已经包含这三个特性了,例如 Sm ...
- sql 语句中的 (+) 是什么意思?
在select语句中(+)指的是外连接,是连接查询的一种方法.例:select t1.*,t2.* from dept t1,emp t2 where t1.deptno=t2.deptno(+);其 ...
- POJ2142 The Balance (扩展欧几里德)
本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia The Balance 题目大意 你有一个天平(天平左右两边都可以放砝码)与重量为a,b(1<= ...