1 开发哲学
Beautiful is better than ugly.(美丽优于丑陋)
Explicit is better than implicit.(直白优于含蓄)
Simple is better than complex.(简单优于复杂)
Complex is better than complicated.(复杂优于繁琐)
Readability counts.(可读性很重要)
2 遵循的协议
Requests 的发布许可为 Apache2 License.
因为项目发行于 GPL 协议之后,就不能用于任何本身没开源的商业产品中。
MIT、BSD、ISC、Apache2 许可都是优秀的替代品,它们允许你的开源软件自由应用在私有闭源软件中。
3 requests信息
requests是一个Python的网络请求库,和urllib、httplib之流相比起来最大的优点就是好用。requests还支持https验证并且是线程安全的。
下载地址
http://www.u148.net/u/512996/
安装后在eclipse如果还没有识别,需要在PYTHON解释器中加上
Properties-> PyDev Interpreter
Click here to configure an Interpreter not list.
4 简单使用
import requests
r = requests.get('https://www.python.org')
r.status_code
print r.status_code
直接执行即可。
或者如下:
import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
print r.json()
5 代码
因为官方提供的项目信息并不是特别多,一个一个解决不免枯燥乏味,为此想到一个实战办法。以具体实例为切入点一点一点阅读。
5.1 Request(主要接口)
示例代码如下:
import requests
r = requests.get('https://github.com/timeline.json')
print r
r = requests.post("http://httpbin.org/post")
print r
r = requests.put("http://httpbin.org/put")
print r
r = requests.delete("http://httpbin.org/delete")
print r
r = requests.head("http://httpbin.org/get")
print r
r = requests.options("http://httpbin.org/get")
print r
我们来学习一下
import 导入我们安装的requests包
然后调用函数get,post,put,delete,head,options
Requests 所有的功能都可以通过以下 7 个方法访问
最后都返回一个Response对象的实例。
5.1.1 get
在api.py文件中,内如如下:
def get(url, params=None, **kwargs):
r"""Sends a GETrequest.
:param url: URL for the new:class:`Request` object.
:param params: (optional)Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
发送GET请求,参数有url,params,kwargs.
url参数Request对象。Params是字典。Kwargs是可选参数,被requests调用。
返回Response对象。
最后调用request函数。
5.1.2 post
def post(url, data=None, json=None, **kwargs):
r"""Sends a POSTrequest.
:param url: URL for the new:class:`Request` object.
:param data: (optional) Dictionary (willbe form-encoded), bytes, or file-like object to send in the body of the:class:`Request`.
:param json: (optional) jsondata to send in the body of the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
5.1.3 put
def put(url, data=None, **kwargs):
r"""Sends a PUTrequest.
:param url: URL for the new:class:`Request` object.
:param data: (optional) Dictionary (willbe form-encoded), bytes, or file-like object to send in the body of the:class:`Request`.
:param json: (optional) jsondata to send in the body of the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('put', url, data=data, **kwargs)
和get基本一致。
5.1.4 delete
def delete(url, **kwargs):
r"""Sends a DELETErequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('delete', url, **kwargs)
和get基本一致。
5.1.5 head
def head(url, **kwargs):
r"""Sends a HEADrequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', False)
return request('head', url, **kwargs)
和get基本一致。
5.1.6 options
def options(url, **kwargs):
r"""Sends a OPTIONSrequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('options', url, **kwargs)
和get基本一致。
(完)
握住佢纤细有力嘅脚腕。祈星,因对方嘅举动猛然一惊,佢迅捷地收脚,转而一记嘅右钩拳袭向对方嘅肚腩。
然隋宇谦似系一早预料到对