Python Requests库
背景
Requests is an elegant and simple HTTP library for Python, built for human beings.
Requests是一个优雅简洁的Python HTTP库,给人类使用。
Requests使用urllib3,所以拥有它的所有特性。
支持python 2.6 – 3.5 。
不得不说,超级喜欢他们家的文档啊,很pythonic。
安装
pip install requests
发送请求
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r = requests.post("http://httpbin.org/post")
>>> 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")
GET
不带参数的GET:
>>> r = requests.get('https://github.com/timeline.json')
带有参数的GET:
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload)
>>> print(r.url)
http://httpbin.org/get?key2=value2&key1=value1 >>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
>>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3
带有header的GET:
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
设置连接的超时时间:
>>> requests.get('http://github.com', timeout=0.001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
带有cookie的GET:
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working') >>> r = requests.get(url, cookies=cookies)
>>> r.text
'{"cookies": {"cookies_are": "working"}}'
POST
不带参数的POST:
>>> r = requests.post("http://httpbin.org/post")
带有参数的POST:
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
Response
URL:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json') >>>r.url
'https://github.com/timeline.json'
返回的内容:
>>> r.text //以encoding自动解码出来的结果
u'[{"repository":{"open_issues":0,"url":"https://github.com/... >>> r.content //返回的二进制相应内容
b'[{"repository":{"open_issues":0,"url":"https://github.com/... >>> r.raw //返回的原始内容
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810> >>> r.json() //以json形式解析
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
使用的编码:
>>> r.encoding
'utf-8'
>>> r.encoding = 'ISO-8859-1' //设置编码
相应的状态码:
>>> r.status_code
200
>>> r.status_code == requests.codes.ok
True
返回的header:
>>> r.headers
{
'content-encoding': 'gzip',
'transfer-encoding': 'chunked',
'connection': 'close',
'server': 'nginx/1.0.4',
'x-runtime': '148ms',
'etag': '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json'
} >>> r.headers['Content-Type']
'application/json' >>> r.headers.get('content-type')
'application/json'
COOKIE:
>>> r.cookies['example_cookie_name']
'example_cookie_value'
Python Requests库的更多相关文章
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- Python——Requests库的开发者接口
本文介绍 Python Requests 库的开发者接口,主要内容包括: 目录 一.主要接口 1. requests.request() 2. requests.head().get().post() ...
- 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...
- Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)
Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...
- Python requests库的使用(一)
requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...
- Python Requests库:HTTP for Humans
Python标准库中用来处理HTTP的模块是urllib2,不过其中的API太零碎了,requests是更简单更人性化的第三方库. 用pip下载: pip install requests 或者git ...
- python requests库学习笔记(下)
1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions #引入exc ...
- python+requests库,接口自动化
1.requests库的使用 requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: “ ...
随机推荐
- c#简单的Json解析类
使用方法: 引用Newtonsoft.Json.dll文件,然后引用命名空间using Newtonsoft.Json.Linq;JsonDome中有实例,照做就行 现在贴上示例代码 using Ne ...
- php错误捕捉
<?php //禁止错误输出 error_reporting(0); //设置错误处理器 set_error_handler('errorHandler'); register_shutdown ...
- Python全栈开发之MySQL(三)视图,存储过程触发器,函数,事务,索引
一:视图 1:什么是视图? 视图是指存储在数据库中的查询的SQL语句,具有简单.安全.逻辑数据独立性的作用及视点集中简化操作定制数据安全性的优点.视图包含一系列带有名称的列和行数据.但是,视图并不在数 ...
- Python之路----数据类型
Python的集成开发环境(IDE):pycharm 数据类型 数字 整数int(integer) 浮点数float 布尔型,只有两个值 真:True 假:False 字符串 列表 元组 字典 一.字 ...
- uublog在线测试demo
http://demo.uublog.me/ 后台 http://demo.uublog.me/admin/ 用户名/密码:admin/admin
- asp.net MVC FileResult在IE下异常的解决办法
var encoding = System.Text.Encoding.UTF8; Response.Charset = encoding.WebName; Response.HeaderEncodi ...
- CSS3 calc() 会计算的属性
calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,你可以使用calc()给元素的border.margin.pading.font-size和width等属性设置动态值. ...
- 二维卷积c代码
二维卷积c代码 二维信号的卷积原理请参考另外一篇文章:http://blog.csdn.net/carson2005/article/details/43702241 这里直接给出参考代码: void ...
- csu 10月 月赛 B 题 Scoop water
一个卡特兰数的应用: 卡特兰数主要有以下几个用途: 1.不同的出栈入栈数: 2.n个点组成的不同的二叉树的数目: 3.凸多边形的三角剖分划分: 4.括号化问题: 通项公式是:h(n) = C(2n-2 ...
- Codeforces Round #197 (Div. 2) : E
看了codeforces上的大神写的题解之后,才知道这道题水的根本! 不过相对前面两题来说,这道题的思维要难一点: 不过想到了水的根本,这题也真心不难: 方法嘛,就像剥洋葱一样,从外面往里面剥: 所以 ...