随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

批量下载RFC文档(python实现)

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

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

复制代码
 1 #! /usr/bin/python
2 '''
3 File : getRFC.py
4 Author : Mike
5 E-Mail : Mike_Zhang@live.com
6 '''
7 import urllib,os,shutil,time
8
9 def downloadHtmlPage(url,tmpf = ''):
10 i = url.rfind('/')
11 fileName = url[i+1:]
12 if tmpf : fileName = tmpf
13 print url,"->",fileName
14 urllib.urlretrieve(url,fileName)
15 print 'Downloaded ',fileName
16 time.sleep(0.2)
17 return fileName
18
19 # http://www.networksorcery.com/enp/rfc/rfc1000.txt
20 # http://www.networksorcery.com/enp/rfc/rfc6409.txt
21 if __name__ == '__main__':
22 addr = 'http://www.networksorcery.com/enp/rfc'
23 dirPath = "RFC"
24 #startIndex = 1000
25 startIndex = int(raw_input('start : '))
26 #endIndex = 6409
27 endIndex = int(raw_input('end : '))
28 if startIndex > endIndex :
29 print 'Input error!'
30 if False == os.path.exists(dirPath):
31 os.makedirs(dirPath)
32 fileDownloadList = []
33 logFile = open("log.txt","w")
34 for i in range(startIndex,endIndex+1):
35 try:
36 t_url = '%s/rfc%d.txt' % (addr,i)
37 fileName = downloadHtmlPage(t_url)
38 oldName = './'+fileName
39 newName = './'+dirPath+'/'+fileName
40 if True == os.path.exists(oldName):
41 shutil.move(oldName,newName)
42 print 'Moved ',oldName,' to ',newName
43 except:
44 msgLog = 'get %s failed!' % (i)
45 print msgLog
46 logFile.write(msgLog+'\n')
47 continue
48 logFile.close()
复制代码

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

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

posted on   Mike_Zhang  阅读(2753)  评论(1编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2012年1月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示