API定义:
urllib.request.urlretrieve(url,filename=None,reporthook=None, data=None)
利用urlretrieve() 将数据下载到本地。
- 参数 finename 指定了保存本地路径(如果参数未指定,urllib会生成一个临时文件保存数据。)
 
- 参数 reporthook 是一个回调函数,当连接上服务器、以及相应的数据块传输完毕时会触发该回调,我们可以利用这个回调函数来显示当前的下载进度。
 
- 参数 data 指 post 到服务器的数据,该方法返回一个包含两个元素的(filename, headers)元组,filename 表示保存到本地的路径,header 表示服务器的响应头。
用法:
>>> import urllib.request
>>>local_filename,headers=urllib.request.urlretrieve('http://python.org/')
>>> html = open(local_filename)
>>> html.close()
注意:当html=open(local_filename),然后lines=html.readlines()时可能会出现unicode错误
处理方法:html=open(local_filename,'utf-8')这样就会解决unicode问题。
例子:抓取web页面

#coding:utf-8
from urllib.request import urlretrieve def firstNonBlank(lines):
for eachLine in lines:
if not eachLine.strip():
continue
else:
return eachLine def firstLast(webpage):
f=open(webpage,encoding='utf-8')
lines=f.readlines()
f.close()
print(firstNonBlank(lines))
lines.reverse()
print(firstNonBlank(lines)) def download(url='http://www.baidu.com',process=firstLast):
try:
retval=urlretrieve(url)[0]
except IOError:
retval=None
if retval:
process(retval) if __name__=="__main__":
download()

Python中urlretrieve函数的更多相关文章

  1. Python urllib urlretrieve函数解析

    Python urllib urlretrieve函数解析 利用urllib.request.urlretrieve函数下载文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Ur ...

  2. Python中split()函数的用法及实际使用示例

    Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...

  3. Python中利用函数装饰器实现备忘功能

    Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下   " ...

  4. python中range()函数的用法

    python中range()函数可创建一个整数列表,一般用在for循环中. range()函数语法: range(start,stop[,step]) 参数说明: star: 计数从star开始.默认 ...

  5. Python 中的函数

    学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...

  6. python中format函数

    python中format函数用于字符串的格式化 通过关键字 1 print('{名字}今天{动作}'.format(名字='陈某某',动作='拍视频'))#通过关键字 2 grade = {'nam ...

  7. Python中readline()函数 去除换行符

    从Python中readline()函数读取的一行内容中含有换行符\n,很多时候我们需要处理不含有换行符的字符串,此时就要去掉换行符\n. 方法是使用strip()函数. 例子如下: f = open ...

  8. Python中int()函数的用法浅析

      int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int)  Help on class int in module __builti ...

  9. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

随机推荐

  1. libimobiledevice安装步骤

    https://github.com/libimobiledevice/libimobiledevice libimobiledevice安装指南,你还不知道libimobiledevice为何物,赶 ...

  2. Note_Master-Detail Application(iOS template)_03_main.m

    // main.m #import <UIKit/UIKit.h>//UIKitk框架提供一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面( UI )接口.应用程序 ...

  3. bistu新生-1005

    #include "stdio.h"#include "string.h"int main(){ char ku[]={'0','1','2','3','4', ...

  4. 无法为表空间 ***中的段创建 INITIAL 区

    这是由于表空间不足引起的. 具体错误: 解决方案:扩展表空间

  5. Java custom annotations

    Custom annotation definition is similar as Interface, just with @ in front. Annotation interface its ...

  6. Packages

    Packages are a way of structuring Python's module namespace by using "dotted module names" ...

  7. iOS数据持久化

    在iOS中,实现数据持久化一般分为4大种: 1.属性列表 2.对象归档 3.SQLite 4.Core Data 一.属性列表 NSUserDefaults类的使用和NSKeyedArchiver有很 ...

  8. 如何用JS获取ASP.net中的textbox的值 js获不到text值

    <tr>                        <td class="table_body" style="width: 10%" a ...

  9. URAL 2040 (回文自动机)

    Problem Palindromes and Super Abilities 2 (URAL2040) 题目大意 给一个字符串,从左到右依次添加,询问每添加一个字符,新增加的回文串数量. 解题分析 ...

  10. show master status empty解决方案

    The following MySQL error might occur if you are using MySQL replication and binary logs. mysql> ...