2015-01-02 95 views
3

我想用python下載視頻。我嘗試過使用youtube-dl,但我不想支持下載視頻的網站。如何使用Python下載視頻。首先,我試圖通過以下鏈接http://www.kmcgraphics.com/bits-of-code/how-to-get-the-direct-url-path-to-flv-video-files-on-youtube-for-free/獲取我想要從keepvid.com下載的視頻的直接鏈接。我得到以下鏈接http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270當我試圖運行下面的代碼段時,我收到錯誤。從Python的直接URL下載視頻

import urllib 
test=urllib.URLopener() 
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv") 

錯誤:

Traceback (most recent call last): 
File "downl.py", line 14, in <module> 
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv") 
File "/usr/lib/python2.7/urllib.py", line 240, in retrieve 
fp = self.open(url, data) 
File "/usr/lib/python2.7/urllib.py", line 208, in open 
return getattr(self, name)(url) 
File "/usr/lib/python2.7/urllib.py", line 359, in open_http 
return self.http_error(url, fp, errcode, errmsg, headers) 
File "/usr/lib/python2.7/urllib.py", line 376, in http_error 
return self.http_error_default(url, fp, errcode, errmsg, headers) 
File "/usr/lib/python2.7/urllib.py", line 381, in http_error_default 
raise IOError, ('http error', errcode, errmsg, headers) 
IOError: ('http error', 301, 'Moved Permanently', <httplib.HTTPMessage instance at 0x7f094d5d5290>) 

我是新來的Python。所以請幫助我。

回答

6

urllib.URLopener默認情況下不處理重定向

使用urllib.FancyURLopener代替:

import urllib 
test=urllib.FancyURLopener() 
test.retrieve("http://www.animefun.com/dl/googDev.php?url=/108994262975881368074/Po270.flv","testout.flv")