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

yipeiwu_com5年前Python基础

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

finally:
 urllib.urlcleanup()

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

相关文章

Python设计模式中单例模式的实现及在Tornado中的应用

Python设计模式中单例模式的实现及在Tornado中的应用

单例模式的实现方式 将类实例绑定到类变量上 class Singleton(object): _instance = None def __new__(cls, *args...

python中对list去重的多种方法

今天遇到一个问题,在同事随意的提示下,用了 itertools.groupby 这个函数。不过这个东西最终还是没用上。 问题就是对一个list中的新闻id进行去重,去重之后要保证顺序不变...

如何使用Python的Requests包实现模拟登陆

如何使用Python的Requests包实现模拟登陆

前段时间喜欢用python去抓一些页面玩,但都基本上都是用get请求一些页面,再通过正则去过滤。 今天试了一下,模拟登陆个人网站。发现也比较简单。读懂本文需要对http协议和http会话...

Python Sphinx使用实例及问题解决

这篇文章主要介绍了Python Sphinx使用实例及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 描述 使用 pip 安...

Python中的random()方法的使用介绍

 random()方法返回一个随机浮点数r,使得0是小于或等于r 以及r小于1。 语法 以下是random()方法的语法: random ( ) 注意:此函数是无法直...