学习python示例:实现保存网页到本地
代码如下:
#coding=utf-8
__auther__ = ‘xianbao’
import urllib
import os
def reporthook(blocks_read, block_size, total_size):
if not blocks_read:
print ‘打开连接’
return
if total_size 0:
print “%d正在读取(%dbytes完成)”%(blocks_read, blocks_read * block_size)
else:
amout_read = block_size * blocks_read
print ‘%d正在读取,%d/%d’%(blocks_read, amout_read, total_size)
return
try:
filename, msg = urllib.urlretrieve(‘https://www.jb51.net/’, reporthook=reporthook)
print ‘文件是:’, filename
print ‘头文件是’
print msg
print ‘删除前的文件地址:’, os.path.exists(filename)
finally:
urllib.urlcleanup()
print ‘文件依然存在:’, os.path.exists(filename)