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呢?官方文档中是这样说明的: “ ...
随机推荐
- jquery放大镜插件与样式
这是放大镜插件链接,我已经上传到我博客http://files.cnblogs.com/valiant1882331/%E6%94%BE%E5%A4%A7%E9%95%9C%E6%8F%92%E4%B ...
- 采用python获得并修改文件编码(原创)
windows和linux采用了不同的编码,这让很多人伤透了脑经,这里我采用了Python的chardet库获得代码的编码,然后修改编码. 1.首先需要安装chardet库,有很多方式,我才用的是比较 ...
- php练习5——简单的学生管理系统(隐藏控件的使用)
要求: 程序:gradeManage.html和gradeManage.php 结果 注意: 1.使用隐藏控件时,得在不同表单下,不能在同一个表单下: 2. ...
- php练习3——猜拳游戏,评委打分问题
用户与计算机猜拳 程序caiQuan.html和caiQuan.php: 结果: 评委打分问题,去掉一个最低分和最高分,求平均分,并找出最低分和最高分对应第几个评委, 再找出最佳评委(打分最接近 ...
- 阿里云服务器CentOS 5.7(64位)安装配置LAMP服务器(Apache+PHP5+MySQL)
一.快速安装Apache+PHP5+MySql ----------------------------------------------------- 补充:由于163的yum源上只有php5.1 ...
- PHP — php精粹-编写高效的php代码 --- API
1.数据格式 (1)json 示例代码: $jsonData = '[{"title":"The Magic Flute","time":1 ...
- linux系统文件的颜色代表的意思
1.蓝色代表目录 2.绿色代表可执行文件 3.红色代表可压缩文件 4.白色代表其他文件 5.浅蓝色代表链接文件 6.黄色代码设备 7.红色闪烁表示链接的文件有问题
- Winform与WPF对话框(MessageBox, Dialog)之比较
Winform:使用System.Windows.Forms命名空间中相应控件; WPF则调用Microsoft.Win32. MessageBox: // WinForm private void ...
- Entity Framework Code First 数据迁移
需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...
- 定位 - MapKit - 基本使用
/** * Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Co ...