背景

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。

Requests: 让 HTTP 服务人类

快速上手

开发接口

Requests github

安装

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库的更多相关文章

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

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

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

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

  3. Python——Requests库的开发者接口

    本文介绍 Python Requests 库的开发者接口,主要内容包括: 目录 一.主要接口 1. requests.request() 2. requests.head().get().post() ...

  4. 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)

    一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...

  5. Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)

    Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...

  6. Python requests库的使用(一)

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

  7. Python Requests库:HTTP for Humans

    Python标准库中用来处理HTTP的模块是urllib2,不过其中的API太零碎了,requests是更简单更人性化的第三方库. 用pip下载: pip install requests 或者git ...

  8. python requests库学习笔记(下)

    1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions        #引入exc ...

  9. python+requests库,接口自动化

    1.requests库的使用 requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: “ ...

随机推荐

  1. linux ptheard 生产者消费者

    ;     {           {          printf(         pthread_mutex_lock(&mutex);            != g_iBufSiz ...

  2. dom 笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. php设计模式:工厂模式

    php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...

  4. TDirectory.IsRelativePath是否相对路径

    使用函数: System.IOUtils.TDirectory.IsRelativePath class function IsRelativePath(const Path: string): Bo ...

  5. HTTP状态码——对照表

    ASCII码介绍: HTTP状态码(HTTP Status Code)用来表示web服务器响应客户端的HTTP状态.主要有一下5种状态类型.1xx    消息2xx    成功3xx    重定向4x ...

  6. Android引导界面

    一.所需素材       很有必要整理一下,里面附带友盟的社会化分享组件,我就不去掉了. 二.代码 import com.umeng.update.UmengUpdateAgent; import a ...

  7. 告别无止境的增删改查--Java代码生成器

    转自:http://www.cnblogs.com/zhuYears/archive/2012/02/29/2373491.html 告别无止境的增删改查--Java代码生成器 有感于马上要做个比较大 ...

  8. Visual Studio快捷键小结

    工欲善其事必先利其器,这句话相信大家都听说过.利其器,就是我们先得有个神器,神器就是VS(号称宇宙第一IDE),有了神奇不会用也是白搭,就像你有了倚天剑和屠龙刀你不会使,它也就是废铁(假设它们是铁做的 ...

  9. python代码优化技巧

    转自:http://www.douban.com/group/topic/31478102/ 这个资料库还有些不错的好文章: http://www.ibm.com/developerworks/cn/ ...

  10. 【java并发】线程同步工具Semaphore的使用

    Semaphore通常用于限制可以访问某些资源(物理或逻辑的)的线程数目,我们可以自己设定最大访问量.它有两个很常用的方法是acquire()和release(),分别是获得许可和释放许可.  官方J ...