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呢?官方文档中是这样说明的: “ ...
随机推荐
- /etc/rc.local ; /etc/init.d ;/etc/profile;/etc/bashrc;~/.bash_profile;~/.bashrc;~/.bash_logout
1. /etc/rc.local 这是使用者自订开机启动程序,把需要开机自动运行的程序写在这个脚本里. 把脚本程序写在/etc/rc.d/init.d/目录下也可以 在完成 run level 3 ...
- OpenCV学习目录(持续更新)
这个暑假开始,需要用到图像处理相关的东西,于是我选择了OpenCV库,这里记录下我的整个学习过程. 参考资料: <OpenCV 2计算机视觉编程手册> 张静 译,科学出版社 1. Linu ...
- PHP、JSP、.NET各自的真正优势是什么
PHP的优势在于, 跨平台, 极易部署, 易维护, 为Web而生, 开源社区强大, 文档丰富.至于说3足鼎立, 谈不上, 全球前100万的sites中, 70%是PHP. JSP和Asp..net 也 ...
- cocos2dx3.4 导出节点树到XML文件
l利用cocostudio做UI和场景时,经常要去获取某个节点,cocostudio2.1开始加入了文件的概念,可以创建场景,节点,层等文件,把公用的东西创建到文件里,然后把这个文件拖到场景里使用,达 ...
- 提交App Store注意事项1
1.未遵守苹果iOS APP数据储存指导方针. 如果你的App有离线数据下载功能,尤其需要关注这一点.因为离线数据一般占用存储空间比较大,可以被重新下载和重建,但是用户往往希望系统存储空间紧时也依然能 ...
- docker安装caffe
[最近一直想要学习caffe,但是苦苦纠结于环境安装不上,真的是第一步都迈不出去,还好有docker的存在!下面,对本人如何利用docker安装caffe做以简单叙述,不属于教程,只是记录自己都做了什 ...
- Log4j与common-logging
Log4j与common-logging 总网上搜了些Log4j与common-logging的介绍,记录下. 一.Log4j 1.简介 Log4j是Apache的一个开放源代码项目 使用Log4j ...
- 【技术贴】解决Program Files文件夹消失
好久不写程序了,今天良心发现,就寻找一下自己是否安装了JDK,习惯性的去C盘的Program Files的文件夹下面去找,次奥,没有这个文件夹.好吧.是在玩我么. 于是 打开cmd 输入如下命令 AT ...
- Word Puzzles
poj1204:http://poj.org/problem?id=1204 题意:给你n*m的字符串矩阵,然后p个查询,每个查询会给出一个字符串,然后问你在矩阵中能否通过8个方向搜索到这个字符串,输 ...
- Hibernate save或者persist 后获取主键ID
一个自增长ID的对象被save或者persist后,会返回其主键ID: Department department = new Department(); department.setName(&qu ...