比urllib好用的requests http://www.bsdmap.com/2013/01/13/python-requests/
更多见官方文档:
http://docs.python-requests.org/en/latest/user/quickstart/
http://docs.python-requests.org/en/latest/user/advanced/#advanced 比urllib好用的requests
Python标准库里提供了httplib以及urllib、urllib2,但是学习了好几次,都没有记住(下的功夫不够)。今天崔推荐了一个requests库,看了一下样例,几乎立即就会使用了,所以推荐给大家。
看官方是怎么描述这种情况的:
“Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.
Things shouldn’t be this way. Not in Python.”
http://docs.python-requests.org/en/latest/
可见urllib2确实不太容易使用。
常用功能罗列如下,以便查询。
# 0. 认证、状态码、header、编码、json
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
# 1. 发起请求
import requests
URL="http://www.bsdmap.com/"
r = requests.get(URL)
r = requests.post(URL)
r = requests.put(URL)
r = requests.delete(URL)
r = requests.head(URL)
r = requests.options(URL)
# 4. 二进制内容
You can also access the response body as bytes, for non-text requests:
>>> r.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...
The gzip and deflate transfer-encodings are automatically decoded for you.
For example, to create an image from binary data returned by a request,
ou can use the following code:
>>> from PIL import Image
>>> from StringIO import StringIO
>>> i = Image.open(StringIO(r.content))