在HTTP相关处理中使用python是不必要的麻烦,这包括urllib2模块以巨大的复杂性代价获取综合性的功能。相比于urllib2,Kenneth Reitz的Requests模块更能简约的支持完整的简单用例。
>>> import urllib2 >>> url = 'http://example.test/' >>> response = urllib2.urlopen(url) >>> response.getcode() 200 >>> response.headers.getheader('content-type') 'text/html; charset=utf-8' >>> response.read() 'Hello, world!'
>>> import requests >>> url = 'http://example.test/' >>> response = requests.get(url) >>> response.status_code 200 >>> response.headers['content-type'] 'text/html; charset=utf-8' >>> response.content u'Hello, world!'
2 Requests 自动保存了返回内容,所以你可以读取多次,而不像urllib2.urlopen()那样返回的只是一个类似文件类型只能读取一次的对象。
>>> import urllib2 >>> url = 'http://example.test/secret' >>> password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() >>> password_manager.add_password(None, url, 'dan', 'h0tdish') >>> auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) >>> opener = urllib2.build_opener(auth_handler) >>> urllib2.install_opener(opener) >>> response = urllib2.urlopen(url) >>> response.getcode() 200 >>> response.read() 'Welcome to the secret page!'
一个简单的方法中实例化了2个类,然后组建了第三个类,最后还要装载到全局的urllib2模块中,最后才调用了urlopen,那么那两个复杂的类是什么的
迷惑了吗, 这里所有urllib2的文档 http://docs.python.org/release/2.7/library/urllib2.html
那Requests是怎么样解决同样的问题的呢?>>> import requests >>> url = 'http://example.test/secret' >>> response = requests.get(url, auth=('dan', 'h0tdish')) >>> response.status_code 200 >>> response.content u'Welcome to the secret page!'
>>> response = requests.get(url, auth=('dan', 'wrongPass')) >>> response.ok False