Python實現(xiàn)的批量下載RFC文檔
RFC文檔有很多,有時候在沒有聯(lián)網(wǎng)的情況下也想翻閱,只能下載一份留存本地了。
看了看地址列表,大概是這個范圍:
http://www.networksorcery.com/enp/rfc/rfc1000.txt
...
http://www.networksorcery.com/enp/rfc/rfc6409.txt
哈哈,很適合批量下載,第一個想到的就是迅雷……
可用的時候發(fā)現(xiàn)它只支持三位數(shù)的擴展(用的是迅雷7),我想要下的剛好是四位數(shù)……
郁悶之下萌生自己做一個的想法!
這東西很適合用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打開指定網(wǎng)頁使用requests模塊爬蟲示例詳解
這篇文章主要介紹了Python打開指定網(wǎng)頁使用requests模塊爬蟲的示例,Python?requests是一個常用的HTTP請求庫,可以方便地向網(wǎng)站發(fā)送HTTP請求,并獲取響應結(jié)果,requests模塊比urllib模塊更簡潔,感興趣的朋友可以參考下2024-02-02Python使用pickle模塊存儲數(shù)據(jù)報錯解決示例代碼
這篇文章主要介紹了Python使用pickle模塊存儲數(shù)據(jù)報錯解決示例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01利用Hyperic調(diào)用Python實現(xiàn)進程守護
這篇文章主要為大家詳細介紹了利用Hyperic調(diào)用Python實現(xiàn)進程守護,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01Python多線程編程(三):threading.Thread類的重要函數(shù)和方法
這篇文章主要介紹了Python多線程編程(三):threading.Thread類的重要函數(shù)和方法,本文講解了線程名稱、join方法、setDaemon方法等內(nèi)容,需要的朋友可以參考下2015-04-04