python爬取網站網頁并下載下傳到D盤下test/index.html檔案:
#coding=utf-8
import urllib
def callbackfunc(blocknum, blocksize, totalsize):
'''回調函數
@blocknum: 已經下載下傳的資料塊
@blocksize: 資料塊的大小
@totalsize: 遠端檔案的大小
'''
percent = 100.0 * blocknum * blocksize / totalsize
if percent > 100:
percent = 100
print "%.2f%%"% percent
url = 'http://www.sina.com.cn'
local = 'd:\\test\index.html'
urllib.urlretrieve(url, local, callbackfunc)
print u'下載下傳完畢去D盤下test/index.html檢視'