背景

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. 身份证js验证

    <script type="text/javascript"> //--身份证号码验证-支持新的带x身份证 function isIdCardNo(num) { var ...

  2. 初识pngdrive

    初识是第一次认识的意思,类似的词还有初见.初遇.初心.初愿.初恋.初吻……梦里相见如初识,很美好的感觉.同样,今天我们要认识的也是一个比较神奇美妙的东西,至少对于程序员来说. 我曾经尝试过很多文件加密 ...

  3. javascript 写策略模式,商场收银打折优惠策略

    [Decode error - output not utf-8] ----------------------------- 购物清单 方便面 : 100 x 50 = 5000 | 4000 菊花 ...

  4. PHP小记录

    正的framework(大量使用)      thinkphp(部分使用)      cakephpyii(极少使用) [一]函数    1:函数的声明:每个函数的第一行都是函数开头,有声明函数的关键 ...

  5. CentOS 6.4 播放avi格式的视频文件

    1. 需要先进行相关的yum源的导入: rpm -Uhv http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/rpmforge-release-0. ...

  6. 【WPF】逻辑树和视觉树

    WPF中提供了遍历逻辑树和视觉树的辅助类:System.Windows.LogicalTreeHelper和 System.Windows.Media.VisualTreeHelper. 注意遍历的位 ...

  7. myeclipse启动项目时报:An internal error occurred during: "Launching TestSpring on Tomcat 7.x". java.lang.NullPointerException 解决方法

    如果出现了上述的错误按照如下的3个步骤解决: 1.首先关闭MyEclipse工作空间. 2.然后删除工作空间下的 “/.metadata/.plugins/org.eclipse.core.runti ...

  8. iOS oc 中的闭包

    //闭包 NSString* s =@"123"; void (^block)() = ^() { NSLog(@"%@",s); }; block();// ...

  9. XMLHttpRequest 使用概括

    ***********************************************XMLHttpRequest对象初始化:********************************* ...

  10. Android Studio 打包及引用 aar

    Android Studio 打包及引用 aar 1. 简述 在比较大的 Android 项目的开发中,我们经常会遇到工程.jar 包等等之间相互引用的方式.一般我们通过在 gradle 文件中配置依 ...