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 */ ...
随机推荐
- 用一个小的例子来说明为什么TCP采用三次握手才能保证连接成功
关于TCP的三次握手,有很多朋友还在疑惑为什么是三次,而不是两次,LZ想了一下用一个例子来说明最好. 场景: 办公室有一名前台(服务器),若干业务员(fork出来的进程) 客户(客户端) 为什么我 ...
- CorelDRAW X6+PhotoZoom这组合,无敌了啊!
520就这样毫无察觉的过去了,对于额这种单身狗,额表示,什么520,什么情人节,统统略过,,可是,可是,即便这样,还是硬生生的吃了一把来势凶猛的远在天际的狗粮,当我看到CorelDRAW X6和Pho ...
- ajax请求及遍历
$(".btn").click(function(){ $.ajax({ type:"POST", url:"../../js/info.json&q ...
- 进程线程之pid,tid
Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其i ...
- Day 05 流程控制
流程控制 if 判断 语法: if if...else if...elif...else if判断是干什么的呢?if判断其实是在模拟人做判断.就是说如果这样干什么,如果那样干什么. if...else ...
- pytorch 1 torch_numpy, 对比
import torch import numpy as np details about math operation in torch can be found in: http://pytorc ...
- js内置对象——Math
Math()是JavaScript的内置对象(对于内置对象的理解,会单独写文章说明),包含了很多数学相关的方法: 常用方法: 1 Math.ceil(x) //取最近的最大整数返回 2 Math.fl ...
- 使用docker搭建hadoop分布式集群
使用docker搭建部署hadoop分布式集群 在网上找了非常长时间都没有找到使用docker搭建hadoop分布式集群的文档,没办法,仅仅能自己写一个了. 一:环境准备: 1:首先要有一个Cento ...
- C++11新特性应用--介绍几个新增的便利算法(用于分区的几个算法)
今天继续. C++11新增的关于Non-modifying sequence operations和Modifying sequence operations的算法已经写了.具体信息见之前的博客. 以 ...
- 【HDOJ 5407】 CRB and Candies (大犇推导
pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...