欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)的批量下載RFC文檔

 更新時間:2015年03月10日 11:17:52   投稿:junjie  
這篇文章主要介紹了Python實現(xiàn)的批量下載RFC文檔,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下

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、電子書等等。

好,就這些了,希望對你有幫助。

相關文章

最新評論