天天看点

python爬取网页并下载

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查看'