python requests的使用说明
#GET参数实例
requests.get('http://www.dict.baidu.com/s', params={'wd': 'python'})
#或
url = 'http://www.baidu.com'
payload = {'key1': 'value1', 'key2': 'value2'}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
res1 = requests.get(url, params=payload, headers=headers, timeout=1) #POST参数实例
requests.post('http://www.itwhy.org/wp-comments-post.php', data={'comment': '测试post'})
files = {'file': open('touxiang.png', 'rb')} #用于发送文件的post属性
files = {'file': ('xxxx,jpg',open('/home/lyb/sjzl.mpg','rb'))} #设置文件名
#或
url = 'http://www.baidu.com'
data={"user":"user","password":"pass"}
res2 = requests.post(url1, data=data, headers=headers ,files=files)
POST发送JSON数据:
import json r = requests.post('https://api.github.com/some/endpoint', data=json.dumps({'some': 'data'})) print(r.json()) r = requests.get('http://ip.taobao.com/service/getIpInfo.php?ip=122.88.60.28')
print (r.json()['data']['country']) 添加代理: proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://www.zhidaow.com", proxies=proxies) 一些操作requests返回值的方法: r.text #字符串方式的响应体,会自动根据响应头部的字符编码进行解码
r.content #获得二进制响应内容
r.raw #获得原始响应内容,需要stream=True
r.raw.read(50)
type(r.text) #返回解码成unicode的内容
r.url
r.history #追踪重定向
r.cookies
r.cookies['example_cookie_name']
r.headers #以字典对象存储服务器的响应头,但该字典比较特殊,字典键不区分大小写,若键不存在返回None
r.headers['Content-Type']
r.headers.get('content-type')
r.json #讲返回内容编码为json
r.encoding #返回内容编码
r.status_code #返回http状态码
r.raise_for_status() #返回错误状态码
若编码出错,则r.text.encode('utf-8') #初始化一个session对象
s = requests.Session() #使用这个session对象来进行访问
prepped1 = requests.Request('POST', url1,
data=data,
headers=headers
).prepare()
s.send(prepped1)
#或 r = s.post(url,data = user)
其他的一些访问方式:
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options("http://httpbin.org/get") json: json数据传到requests的body
headers: HTTP Headers的字典传到requests的header
cookies: 可以使用字典或者CookieJar object
files: 字典{'name': file-tuple} 来实现multipart encoding upload, 2参数元组 ('filename', fileobj), 3参数元组 ('filename', fileobj, 'content_type')或者 4参数元组 ('filename', fileobj, 'content_type', custom_headers), 其中'content-type' 用于定于文件类型和custom_headers文件的headers
auth: Auth元组定义用于Basic/Digest/Custom HTTP Auth
timeout: 连接等待时长
allow_redirects: 布尔型, True代表POST/PUT/DELETE只有的重定向是允许的
proxies: 代理的地址
verify: 用于认证SSL证书
stream: False代表返回内容立刻下载
cert: String代表ssl client证书地址(.pem) Tuple代表('cert', 'key')键值对
python requests的使用说明的更多相关文章
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- Python requests库的使用(一)
requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...
- Python requests模拟登录
Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- Python requests 安装与开发
Requests 是用Python语言编写HTTP客户端库,跟urllib.urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求, ...
- Python+Requests接口测试教程(1):Fiddler抓包工具
本书涵盖内容:fiddler.http协议.json.requests+unittest+报告.bs4.数据相关(mysql/oracle/logging)等内容.刚买须知:本书是针对零基础入门接口测 ...
- python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)
python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...
- 转载:python + requests实现的接口自动化框架详细教程
转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)
可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...
随机推荐
- Spring.NET学习笔记——目录(原)
目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...
- Zabbix-3.0.3实现微信(WeChat)告警
导读 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告 ...
- std__vector介绍
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...
- IIS中,当文件夹被删除时,防止应用程序重启的解决办法
如果你曾经修改了ASP.NET应用程序(dll文件),与修改了bin文件夹或Web.config文件(添加/删除/重命名的文件等),而该网 站在运行,你可能已经注意到,这将导致在AppDomain的重 ...
- 170321、Spring+Quartz实现定时任务的配置方法
Quartz是Java版开源定时调度器. 核心概念: Job 表示一个工作,要执行的具体内容.此接口中只有一个方法 void execute(JobExecutionContext context): ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- easyui tree操作
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- SpringCloud 入门
1. 入门概述 SpringBoot专注于快速方便的开发单个个体微服务; SpringCloud:关注全局的微服务协调治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来, 为各个微 ...
- 转!mysql备份与还原数据库
备份数据库:1) mysqldump -uroot -p db_name > 20181018_preprod_bak.sql2) 输入数据库密码 还原数据库:1. 系统命令行:mysqladm ...
- git学习------>Git 分支管理最佳实践
ps:本文转载于 : https://www.ibm.com/developerworks/cn/java/j-lo-git-mange/index.html Git 是目前最流行的源代码管理工具.大 ...