python下載文件的三種方法


Python開發中時長遇到要下載文件的情況,最常用的方法就是通過Http利用urllib或者urllib2模塊。

當然你也可以利用ftplib從ftp站點下載文件。此外Python還提供了另外一種方法requests

下面來看看三種方法是如何來下載zip文件的:
方法一:

import urllib 
import urllib2 
import requests
print "downloading with urllib" 
url = 'http://***/test/demo.zip' 
print "downloading with urllib"
urllib.urlretrieve(url, "demo.zip")

 

方法二:

import urllib2
print "downloading with urllib2"
url = 'http://***/test/demo.zip' 
f = urllib2.urlopen(url) 
data = f.read() 
with open("demo2.zip", "wb") as code: 
code.write(data)

 


方法三:

import requests 
print "downloading with requests"
url = 'http://***/test/demo.zip' 
r = requests.get(url) 
with open("demo3.zip", "wb") as code:
code.write(r.content)

 

看起來使用urllib最為簡單,一句語句即可。當然你可以把urllib2縮寫成:

f = urllib2.urlopen(url) 
with open("demo2.zip", "wb") as code:
code.write(f.read())

==========================================python requests======

在HTTP相關處理中使用python是不必要的麻煩,這包括urllib2模塊以巨大的復雜性代價獲取綜合性的功能。相比於urllib2,Kenneth Reitz的Requests模塊更能簡約的支持完整的簡單用例

簡單的例子:
想象下我們試圖使用get方法從http://example.test/獲取資源並且查看返回代碼,content-type頭信息,還有response的主體內容。這件事無論使用urllib2 或者Requests都是很容易實現的。
urllib2:

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!'

 

Requests:

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!這兩種方法很相似,相對於urllib2調用方法讀取response中的屬性信息,Requests則是使用屬性名來獲取對應的屬性值。

兩者還有兩個細微但是很重要的差別:
1. Requests 自動的把返回信息有Unicode解碼
2. Requests 自動保存了返回內容,所以你可以讀取多次,而不像urllib2.urlopen()那樣返回的只是一個類似文件類型只能讀取一次的對象。

第二點是在python交互式環境下操作代碼很令人討厭的事情

一個復雜一點的例子:現在讓我們嘗試下復雜點得例子:使用GET方法獲取http://foo.test/secret的資源,這次需要基本的http驗證。使用上面的代碼作為模板,好像我們只要把urllib2.urlopen() 到requests.get()之間的代碼換成可以發送username,password的請求就行了

這是urllib2的方法:

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是怎么樣解決同樣的問題的呢?

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!'


只是在調用方法的時候增加了一個auth關鍵字函數
我敢打賭你不用查文檔也能記住。

錯誤處理 Error HandlingRequests 對錯誤的處理也是很非常方面。如果你使用了不正確的用戶名和密碼,urllib2會引發一個urllib2.URLError錯誤,然而Requests 會像你期望的那樣返回一個正常的response對象。只需查看response.ok的布爾值便可以知道是否登陸成功。

response = requests.get(url, auth=('dan', 'wrongPass'))
response.ok

-- False

其他的一些特性:
* Requests對於HEAD, POST, PUT, PATCH, 和 DELETE方法的api同樣簡單
* 它可以處理多部分上傳,同樣支持自動轉碼
* 文檔更好
* 還有更多

Requests 是很好的,下次需要使用HTTP時候可以試試。





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM