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除法负数商的取整方式与C++不同 python: 5 / -2 = -3 若想和C++行为相同,可以使用 int(operator.truediv(num1, num2...

python如何解析配置文件并应用到项目中

配置文件的类型 通常自动化测试中的配置文件是以.ini 和 .conf 为后缀的文件 配置文件的组成 1.section 2.option 3.value 配置文件的格式 [s...

Python读取YAML文件过程详解

这篇文章主要介绍了Python读取YAML文件过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 YAML语法 学习手册 Pyth...

Python PO设计模式的具体使用

Python PO设计模式的具体使用

无规矩不成方圆。编写代码也是,如果没有大概的框架,管理代码将会是一件很头疼的事。 先看看笔者以前写的python脚本: 如果只有一个用例,这样看着好像挺整洁的。但是当用例越来越多后,如...

python中使用百度音乐搜索的api下载指定歌曲的lrc歌词

这次这个真的是干货哦,昨晚弄了半晚上,,,,从8点吃完饭就开始写,一直到了快12点才弄好,,,新手,伤不起呀。。。。 先简单的说下吧,百度提供了一个音乐搜索的api,你想百度请求类似于...