本文介紹了幾種常用的python下載文件的方法,具體使用到了htttplib2,urllib等包,希望對大家有幫忙。
1.簡單文件下載
使用htttplib2,具體代碼如下:
1
2
3
4
5
6
7
|
h = httplib2.Http() url = 'http://www.ythuaji.com.cn/ip.zip' resp, content = h.request(url) if resp[ 'status' ] = = '200' : with open (filename, 'wb' ) as f: f.write(content) |
使用urllib,具體代碼如下:
1
2
|
filename = urllib.unquote(url).decode( 'utf8' ).split( '/' )[ - 1 ] urllib.urlretrieve(url, filename) |
2.較大文件下載
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
def down_file(): url = "http://www.ythuaji.com.cn/download.abc" file_name = url.split( '/' )[ - 1 ] u = urllib2.urlopen(url) f = open (file_name, 'wb' ) meta = u.info() file_size = int (meta.getheaders( "Content-Length" )[ 0 ]) file_size_dl = 0 block_sz = 8192 while True : buffer = u.read(block_sz) if not buffer : break file_size_dl + = len ( buffer ) f.write( buffer ) f.close() |
在獲取下載文件名的過程中,可以解析url,代碼如下:
1
2
3
4
|
scheme, netloc, path, query, fragment = urlparse.urlsplit(url) filename = os.path.basename(path) if not filename: filename = 'downloaded.file' |
3.端點續(xù)傳下載
在使用HTTP協(xié)議進行下載的時候只需要在頭上設置一下Range的范圍就可以進行斷點續(xù)傳下載,當然,首先服務器需要支持斷點續(xù)傳。
利用Python的urllib2模塊完成斷點續(xù)傳下載的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/usr/bin/python # -*- coding: UTF-8 -* ''' Created on 2013-04-15 Created by RobinTang A demo for Resuming Transfer ''' import urllib2 req = urllib2.Request( 'http://www.python.org/' ) req.add_header( 'Range' , 'bytes=0-20' ) # set the range, from 0byte to 19byte, 20bytes len res = urllib2.urlopen(req) data = res.read() print data print '---------' print 'len:%d' % len (data) |