Batch download of RFC documents implemented by Python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.