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)

相关文章

Python数据拟合与广义线性回归算法学习

Python数据拟合与广义线性回归算法学习

机器学习中的预测问题通常分为2类:回归与分类。 简单的说回归就是预测数值,而分类是给数据打上标签归类。 本文讲述如何用Python进行基本的数据拟合,以及如何对拟合结果的误差进行分析...

对Python 3.2 迭代器的next函数实例讲解

在python中,使用iter函数可以获得有序聚合类型的迭代器,我个人将迭代器理解为带有next指针的单向链表,获取到的迭代器为链表的表头,表头内容为空,next指针指向有序聚合类型的第...

python 多进程并行编程 ProcessPoolExecutor的实现

使用 ProcessPoolExecutor from concurrent.futures import ProcessPoolExecutor, as_completed im...

Python自动扫雷实现方法

本文实例讲述了Python自动扫雷实现方法。分享给大家供大家参考。具体如下: #pyWinmineCrack.py # coding: utf-8 import win32gui i...

Python如何基于smtplib发不同格式的邮件

这篇文章主要介绍了Python如何基于smtplib发不同格式的邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 常用邮箱SMTP、...