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、電子書等等。
好,就這些了,希望對你有協助。