python之urllib模块urlretrieve()函数初窥
来源:互联网 发布:公交数据接口 编辑:程序博客网 时间:2024/05/19 06:15
今天在使用python的库urllib做实验的时候接触到了urlretrieve()这个函数,个人感觉很有意思,可能是因为之前一直都没有使用过这样简洁的函数,urllib模块可以很容易的获取互联网上页面的html内容,之后可以利用re模块提供的正则表达式来提取自己需要的信息,在需要从网上下载数据或者材料的时候需要编写专门的程序来保存文件。
urlretrieve()这个函数可以直接从互联网上下载文件保存到本地路径下,下面简单来介绍一下这个函数的用法。
在python shell中输入:
help(urllib.urlretrieve)
得到关于这个函数的基本用法信息为:
Help on function urlretrieve in module urllib:urlretrieve(url, filename=None, reporthook=None, data=None)(END)
在这里介绍一下各个参数的意义,进入python官网查看的关于urllib模块的官网文档得到如下解释:
- 参数 finename 指定了保存本地路径(如果参数未指定,urllib会生成一个临时文件保存数据。)
- 参数 reporthook 是一个回调函数,当连接上服务器、以及相应的数据块传输完毕时会触发该回调,我们可以利用这个回调函数来显示当前的下载进度。
- 参数 data 指 post 到服务器的数据,该方法返回一个包含两个元素的(filename, headers)元组,filename 表示保存到本地的路径,header 表示服务器的响应头。
#!/usr/bin/python # -*- coding: utf-8 -*- import osfrom urllib import urlretrievefrom urllib import urlopenfrom bs4 import BeautifulSoupdownloadDirectory = "downloaded"baseUrl = "http://pythonscraping.com"def getAbsoluteURL(baseUrl, source): if source.startswith("http://www."): url = "http://"+source[11:] elif source.startswith("http://"): url = source elif source.startswith("www."): url = source[4:] url = "http://"+source else: url = baseUrl+"/"+source if baseUrl not in url: return None return urldef getDownloadPath(baseUrl, absoluteUrl, downloadDirectory): path = absoluteUrl.replace("www.", "") path = path.replace(baseUrl, "") path = downloadDirectory+path directory = os.path.dirname(path) if not os.path.exists(directory): os.makedirs(directory) return pathhtml = urlopen("http://www.pythonscraping.com")bsObj = BeautifulSoup(html, "html.parser")downloadList = bsObj.findAll(src=True)print '________________________________________________________________________________________________'print downloadListprint len(downloadList)for download in downloadList: fileUrl = getAbsoluteURL(baseUrl, download["src"]) if fileUrl is not None: print '********************************************************************************' print(fileUrl) urlretrieve(fileUrl, getDownloadPath(baseUrl, fileUrl, downloadDirectory))
实验结果如下:
[<script src="http://www.pythonscraping.com/misc/jquery.js?v=1.4.4" type="text/javascript"></script>, <script src="http://www.pythonscraping.com/misc/jquery.once.js?v=1.2" type="text/javascript"></script>, <script src="http://www.pythonscraping.com/misc/drupal.js?nhx1dd" type="text/javascript"></script>, <script src="http://www.pythonscraping.com/sites/all/themes/skeletontheme/js/jquery.mobilemenu.js?nhx1dd" type="text/javascript"></script>, <script src="http://www.pythonscraping.com/sites/all/modules/google_analytics/googleanalytics.js?nhx1dd" type="text/javascript"></script>, <img alt="Home" src="http://www.pythonscraping.com/sites/default/files/lrg_0.jpg"/>, <iframe frameborder="0" height="500px" scrolling="no" src="http://www.oreilly.com/authors/widgets/669.html" width="200px"></iframe>, <img alt="" src="http://pythonscraping.com/img/lrg%20(1).jpg" style="height:394px; width:300px"/>]8http://pythonscraping.com/misc/jquery.js?v=1.4.4http://pythonscraping.com/misc/jquery.once.js?v=1.2http://pythonscraping.com/misc/drupal.js?nhx1ddhttp://pythonscraping.com/sites/all/themes/skeletontheme/js/jquery.mobilemenu.js?nhx1ddhttp://pythonscraping.com/sites/all/modules/google_analytics/googleanalytics.js?nhx1ddhttp://pythonscraping.com/sites/default/files/lrg_0.jpghttp://pythonscraping.com/img/lrg%20(1).jpg
运行之后在程序同级的目录下面出现了downloaded目录文件,打开一看可以看到文件都保存成功,程序很简单,感兴趣的同学也可以拿去运行试试。
接下来使用urlretrieve()来下载几个网页保存到指定路径中去,程序如下:
#!/usr/bin/python # -*- coding: utf-8 -*- import osimport urllibdef callback(dbnum, dbsize, size): '''回调函数 dbnum: 已经下载的数据块 dbsize: 数据块的大小 size: 远程文件的大小 ''' percent = 100.0 * dbnum * dbsize / size if percent > 100: percent = 100 print "%.2f%%"% percent
#将输入的url处理为标准格式,否则会发生解析出错的问题def url_handle(url): if url.startswith('http://www.'): pass else: url = 'http://www.' + url return url if __name__ == '__main__': if not os.path.exists('downloaddata'): os.mkdir('downloaddata') url_list = ['http://www.baidu.com', 'taobao.com', 'http://www.vmall.com'] for one_url in url_list: print '*****************************downloading************************************' url = url_handle(one_url) local = 'downloaddata' + '/' + url.split('.')[-2] + '.html' urllib.urlretrieve(url, local, callback)
得到如下结果:
*****************************downloading************************************
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
*****************************downloading************************************
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
*****************************downloading************************************
-0.00%
-819200.00%
-1638400.00%
-2457600.00%
-3276800.00%
-4096000.00%
-4915200.00%
-5734400.00%
-6553600.00%
-7372800.00%
-8192000.00%
-9011200.00%
-9830400.00%
-10649600.00%
-11468800.00%
-12288000.00%
-13107200.00%
-13926400.00%
-14745600.00%
-15564800.00%
同时在本地看到了保存成功的三个页面的文件,大小分别为:100KB、133KB、150KB
简单的一个小尝试,之后继续学习,欢迎一起交流学习!
- python之urllib模块urlretrieve()函数初窥
- Python urllib模块urlretrieve()详解
- Python urllib模块urlopen()与urlretrieve()详解
- Python urllib模块urlopen()与urlretrieve()详解
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析Python的爬虫函数
- (转)Python urllib模块urlopen()与urlretrieve()详解
- python 下载 保存 图片的urllib.urlretrieve()函数 简单用法
- python urllib urlretrieve学习使用
- Python中urllib的urlretrieve
- python模块之urllib
- Python 之 urllib模块
- Python urllib模块urlopen()【打开网站,写入本地文件】与urlretrieve()【直接下载网站文件到本地】
- urllib.urlretrieve
- urllib.urlretrieve()
- urllib.urlretrieve()
- spring笔记
- Linux 常用命令
- Android Activity的四种launchMode
- ng面试题整理
- android学习之文本显示走马灯
- python之urllib模块urlretrieve()函数初窥
- java反射之Class类
- Linux 安装字体
- 【OpenCV】OpenCV的样本训练
- init
- 奇偶校验
- qt4.8.6解析xml
- UILabel周边出现灰色线
- 离开了公司,你还有什么(节选)