#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的使用说明的更多相关文章

  1. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

  2. Python requests库的使用(一)

    requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...

  3. Python requests模拟登录

    Python requests模拟登录 #!/usr/bin/env python # encoding: UTF-8 import json import requests # 跟urllib,ur ...

  4. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  5. Python requests 安装与开发

    Requests 是用Python语言编写HTTP客户端库,跟urllib.urllib2类似,基于 urllib,但比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求, ...

  6. Python+Requests接口测试教程(1):Fiddler抓包工具

    本书涵盖内容:fiddler.http协议.json.requests+unittest+报告.bs4.数据相关(mysql/oracle/logging)等内容.刚买须知:本书是针对零基础入门接口测 ...

  7. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  8. 转载:python + requests实现的接口自动化框架详细教程

    转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...

  9. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

随机推荐

  1. 权限模块_使用权限_实现主页面的效果_显示左侧菜单&只显示有权限的菜单项

    权限模块__使用权限__实现主页面的效果 HomeAction.java public class HomeAction extends ActionSupport { public String i ...

  2. 实际用户ID,有效用户ID,保存的设置用户ID

    Unix中常见的几个概念,下面做一个解释. 首先需要明确一点,这几个概念都是和进程相关的. real user ID表示的是实际上进程的执行者是谁,effective user ID主要用于校验该进程 ...

  3. 在scrollview中双击定点放大的代码

    双击放大是 iPhone 的一个基本操作,第三方程序里引入这一功能的话,主要是在 scrollview 呈现一张图片或者 PDF 页面时,双击可以放大,主要代码如下 - (void)scrollVie ...

  4. codevs 5964 [SDOI2017]序列计数

     [题解] 官方题解就两句话. 写了三个版本的不同分值代码.看代码吧. 前导1 //f[i][j][1/0]表示长为i,sum mod p=j,是否已经选了质数的方案数 #include<cst ...

  5. java方法的理解、调用栈与异常处理

    一.流程分支 If/else :基于boolean值的双分支 Switch:基于数字(整数.char.byte.枚举).字符串 类型的多分支 Int month =5; Switch 二.方法meth ...

  6. Code Forces 20A BerOS file system

    A. BerOS file system time limit per test 2 seconds memory limit per test 64 megabytes input standard ...

  7. 适配器模式(Adpater)

    一.适配器模式介绍 适配器模式:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的类可以一起工作. 例如: NBA中的球员来自不同国家,而世界标准语言 ...

  8. Spark源码分析 – SparkEnv

    SparkEnv在两个地方会被创建, 由于SparkEnv中包含了很多重要的模块, 比如BlockManager, 所以SparkEnv很重要 Driver端, 在SparkContext初始化的时候 ...

  9. Yii框架2.0的模型

    模型是 MVC 模式中的一部分, 是代表业务数据.规则和逻辑的对象. 可通过继承 [[yii\base\Model]] 或它的子类定义模型类,基类[[yii\base\Model]]支持许多实用的特性 ...

  10. 用Python爬虫爬取广州大学教务系统的成绩(内网访问)

    用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...