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

yipeiwu_com6年前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)

相关文章

浅谈配置OpenCV3 + Python3的简易方法(macOS)

我的电脑本来是有手动CMake+make安装的OpenCV3的,以及系统自带的python2.x,但是现在想用python3+OpenCV3。 安装Python3 brew ins...

python 文件操作删除某行的实例

使用continue跳过本次写循环就可以了 #文本内容 Yesterday when I was young 昨日当我年少轻狂 The tasting of life was swe...

python pandas写入excel文件的方法示例

pandas读取、写入csv数据非常方便,但是有时希望通过excel画个简单的图表看一下数据质量、变化趋势并保存,这时候csv格式的数据就略显不便,因此尝试直接将数据写入excel文件。...

python开发游戏的前期准备

python开发游戏的前期准备

本文章面向有一定基础的python学习者,使用Pygame包开发一款简单的游戏 首先打开命令行,使用PyPI下载Pygame包(输入命令pip install pygame) 打开py...

python内置函数:lambda、map、filter简单介绍

lambda lambda可以理解为一种小函数,但是它是一个表达式,而不是一个语句,所以在def不允许出现的地方仍然可以使用lambda函数,例如list里。但是lambda内只可以执行...