python requests python

说明

Requests allow you to send HTTP/1.1 requests. You can add headers, form data, multipart files, and parameters with simple Python dictionaries, and access the response data in the same way. It’s powered by httplib and urllib3, but it does all the hard work and crazy hacks for you.

安装

多种方式安装,这里采用setuptools

git clone git://github.com/kennethreitz/requests.git
python setup.py install

快速入门

用Requests来创建一个HTTP请求很简单

import requests
r = requests.get('https://www.google.com/search?q=python')

这里创建了一个get请求,同时返回了一个Response对象.当然也可以传递参数的方式发送请求

queryparams = {'q':'python'}
r = requests.get('https://www.google.com/search', params=queryparams)

当然对于其他的HTTP请求类型也同样支持:

r = requests.post('https://www.somesite.com/post')
r = requests.put('https://www.somesite.com/put')
r = requests.delete('https://www.somesite.com/delete')
r = requests.head('https://www.somesite.com/head')
r = requests.options('https://www.somesite.com/options')

Response

Response表示了一个HTTP响应对象,可以通过text的方式返回r.text,也可以是二进制的内容r.content。同样也可以返回原生的内容r.raw.此时是requests.packages.urllib3.response.HTTPResponse对象。

r = requests.get('https://www.google.com/search?q=python',stream=True)
r.raw
>>> <requests.packages.urllib3.response.HTTPResponse object at 0x02367470>

with open('sss.html','wb') as fd:
	for chunk in r.iter_content(1000):
		fd.write(chunk)

上面的例子将请求的内容原生返回并写入了指定的文件。对应返回的内容,也可以指定编码方式r.encoding.下表展示了Response中的属性和方法

status_code返回的HTTP状态码
headers以字典的形式返回响应的Headers,如r.headers['content-type']
raw类文件的形式返回请求对象,需要设置请求参数stream=True
url请求后的最终url
encoding设置解析 r.text的编码
cookies服务端响应的Cookies,CookieJar对象返回,如r.cookies['cookie_name']
iter_content(chunk_size=1, decode_unicode=False)迭代原生的相应数据,见上例
iter_lines(chunk_size=ITER_CHUNK_SIZE, decode_unicode=None)iter_content类似,只是按行读取。在返回大量数据时减少内存占用
content()用byte返回响应的内容
text用unicode返回响应的内容,可通过r.encoding设置编码
json(**kwargs)json格式返回

再看Request

从前面知道提供了get,post,put等7个方法。其实这些都依赖于一个入口requests.request(method, url, **kwargs)来看看一个方法:

def get(url, **kwargs):
    kwargs.setdefault('allow_redirects', True)
    return request('get', url, **kwargs)

而在主要的入口方法request(method, url, **kwargs)中有如下的参数:

method必选HTTP方法,get,post,put,delete
url必选当前请求的URL
params可选当前请求的查询参数(get),字典或byte格式
data可选Form表单的请求参数(post),可以是字典、byte或文件
headers可选请求的headers
cookies可选请求的cookies
files可选迭代原生的相应数据,见上例
auth可选Auth tuple to enable Basic/Digest/Custom HTTP Auth
timeout可选超时设置
allow_redirects可选Boolean. Set to True if POST/PUT/DELETE redirect following is allowed
proxies可选Dictionary mapping protocol to the URL of the proxy
verify可选 if True, the SSL cert will be verified. A CA_BUNDLE path can also be provided
stream可选if False, the response content will be immediately downloaded
cert可选if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair


blog comments powered by Disqus