Python实现的批量下载RFC文档


Posted in Python onMarch 10, 2015

RFC文档有很多,有时候在没有联网的情况下也想翻阅,只能下载一份留存本地了。
看了看地址列表,大概是这个范围:
http://www.networksorcery.com/enp/rfc/rfc1000.txt
...
http://www.networksorcery.com/enp/rfc/rfc6409.txt

哈哈,很适合批量下载,第一个想到的就是迅雷……
可用的时候发现它只支持三位数的扩展(用的是迅雷7),我想要下的刚好是四位数……
郁闷之下萌生自己做一个的想法!
这东西很适合用python做,原理很简单,代码也很少,先读为快。
代码如下:

#! /usr/bin/python

'''

  File      : getRFC.py

  Author    : Mike

  E-Mail    : Mike_Zhang@live.com

'''

import urllib,os,shutil,time
def downloadHtmlPage(url,tmpf = ''):

    i = url.rfind('/')

    fileName = url[i+1:]

    if tmpf : fileName = tmpf

    print url,"->",fileName

    urllib.urlretrieve(url,fileName)

    print 'Downloaded ',fileName    

    time.sleep(0.2)

    return fileName

    

# http://www.networksorcery.com/enp/rfc/rfc1000.txt

# http://www.networksorcery.com/enp/rfc/rfc6409.txt

if __name__ == '__main__':

    addr = 'http://www.networksorcery.com/enp/rfc'    

    dirPath = "RFC"

    #startIndex = 1000

    startIndex = int(raw_input('start : '))

    #endIndex = 6409

    endIndex = int(raw_input('end : '))

    if startIndex > endIndex : 

        print 'Input error!'        

    if False == os.path.exists(dirPath):

        os.makedirs(dirPath)    

    fileDownloadList = []

    logFile = open("log.txt","w")

    for i in range(startIndex,endIndex+1):

        try:            

            t_url = '%s/rfc%d.txt' % (addr,i)

            fileName = downloadHtmlPage(t_url)

            oldName = './'+fileName

            newName = './'+dirPath+'/'+fileName

            if True == os.path.exists(oldName):

                shutil.move(oldName,newName)

                print 'Moved ',oldName,' to ',newName

        except:

            msgLog = 'get %s failed!' % (i)

            print msgLog

            logFile.write(msgLog+'\n')

            continue

    logFile.close()

除了RFC文档,这个程序稍加修改也可以做其它事情:比如批量下载MP3、电子书等等。

好,就这些了,希望对你有帮助。

Python 相关文章推荐
Python中使用logging模块打印log日志详解
Apr 05 Python
使用requests库制作Python爬虫
Mar 25 Python
python-opencv 将连续图片写成视频格式的方法
Jan 08 Python
python linecache 处理固定格式文本数据的方法
Jan 08 Python
Python第三方库h5py_读取mat文件并显示值的方法
Feb 08 Python
通过shell+python实现企业微信预警
Mar 07 Python
PyTorch学习:动态图和静态图的例子
Jan 06 Python
python3将变量写入SQL语句的实现方式
Mar 02 Python
Window系统下Python如何安装OpenCV库
Mar 05 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
Jun 02 Python
使用Python pip怎么升级pip
Aug 11 Python
python对批量WAV音频进行等长分割的方法实现
Sep 25 Python
Python制作CSDN免积分下载器
Mar 10 #Python
Python Tkinter GUI编程入门介绍
Mar 10 #Python
Python格式化css文件的方法
Mar 10 #Python
Python将xml和xsl转换为html的方法
Mar 10 #Python
Python写的Tkinter程序屏幕居中方法
Mar 10 #Python
Python生成随机密码
Mar 10 #Python
Python获取单个程序CPU使用情况趋势图
Mar 10 #Python
You might like
解析php多线程下载远程多个文件
2013/06/25 PHP
php格式化日期和时间格式化示例分享
2014/02/24 PHP
php导出生成word的方法
2015/12/25 PHP
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
php利用fsockopen GET/POST提交表单及上传文件
2017/05/22 PHP
PHP递归遍历文件夹去除注释并压缩php源代码的方法示例
2018/05/23 PHP
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
js实现按Ctrl+Enter发送效果
2014/09/18 Javascript
js实现鼠标划过给div加透明度的方法
2015/05/25 Javascript
jQuery仅用3行代码实现的显示与隐藏功能完整实例
2015/10/08 Javascript
微信小程序 教程之列表渲染
2016/10/18 Javascript
浅谈jQuery操作类数组的工具方法
2016/12/23 Javascript
angular或者js怎么确定选中ul中的哪几个li
2017/08/16 Javascript
vue-ajax小封装实例
2017/09/18 Javascript
微信小程序显示下拉列表功能【附源码下载】
2017/12/12 Javascript
vue form 表单提交后刷新页面的方法
2018/09/04 Javascript
Javascript之高级数组API的使用实例
2019/03/08 Javascript
python单例模式实例分析
2015/04/08 Python
Django验证码的生成与使用示例
2017/05/20 Python
详解CSS3中@media的实际使用
2015/08/04 HTML / CSS
详解WebSocket跨域问题解决
2018/08/06 HTML / CSS
鼠标滚轮事件和Mac触控板双指事件
2019/12/23 HTML / CSS
AmazeUi Tree(树形结构) 应用小结
2020/08/17 HTML / CSS
类的核心特性有哪些
2014/01/01 面试题
中英文求职信范文
2014/01/27 职场文书
软件部经理岗位职责范本
2014/02/25 职场文书
课堂教学改革实施方案
2014/03/17 职场文书
课外访万家心得体会
2014/09/03 职场文书
党的群众路线教育实践活动心得体会(医院)
2014/11/03 职场文书
2014年银行客户经理工作总结
2014/11/12 职场文书
酒店财务总监岗位职责
2015/04/03 职场文书
销售开票员岗位职责
2015/04/15 职场文书
迎新晚会主持词开场白
2015/05/28 职场文书
汽车车尾标语大全
2015/08/11 职场文书
python自动化八大定位元素讲解
2021/07/09 Python
nginx共享内存的机制详解
2022/03/21 Servers