This article mainly introduces how to download the RFC documentation in batches using Python. This article provides the implementation code directly. For more information, see the RFC documentation, sometimes I want to read it without the internet connection. I can only download one copy and save it locally.
Look at the address list, probably in the following range:
Http://www.networksorcery.com/enp/rfc/rfc1000.txt
...
Http://www.networksorcery.com/enp/rfc/rfc6409.txt
Haha, it is very suitable for batch download. The first thought is thunder ......
When it is available, it is found that it only supports three-digit extension (using Thunder 7). I want to have exactly four digits ......
When I was depressed, I came up with my own idea!
This is very suitable for using python. The principle is very simple, and there are few codes. read it quickly first.
The code is as follows:
The code is as follows:
#! /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/rfc0000d.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 t:
MsgLog = 'get % s failed! '% (I)
Print msgLog
LogFile. write (msgLog + '\ n ')
Continue
LogFile. close ()
In addition to the RFC documentation, this program can also be modified with a little bit: for example, batch downloading MP3 files and e-books.
Okay, that's all. I hope it will help you.