Home >Backend Development >Python Tutorial >python实现保存网页到本地示例

python实现保存网页到本地示例

WBOY
WBOYOriginal
2016-06-16 08:44:491187browse

学习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   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('http://www.jb51.net/', reporthook=reporthook)
 print
 print '文件是:', filename
 print '头文件是'
 print msg
 print '删除前的文件地址:', os.path.exists(filename)

finally:
 urllib.urlcleanup()

 print '文件依然存在:', os.path.exists(filename)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn