python实现保存网页到本地示例
时间:2021-02-07 11:22 作者:admin610456
学习python/' target='_blank'>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
print '文件是:', filename
print '头文件是'
print msg
print '删除前的文件地址:', os.path.exists(filename)
finally:
urllib.urlcleanup()
print '文件依然存在:', os.path.exists(filename)
(责任编辑:admin)