Python-implemented bulk download RFC document _python

Source: Internet
Author: User
Tags rfc

There are a number of RFC documents that sometimes want to flip through without networking, and can only download a copy of the Local.
Look at the address list, which is probably the range:
Http://www.networksorcery.com/enp/rfc/rfc1000.txt
...
Http://www.networksorcery.com/enp/rfc/rfc6409.txt

Haha, very suitable for bulk download, the first thought is the thunderbolt ...
When available, I found that it only supports three-digit extensions (Thunder 7), and I want a four-digit number.
Depressed under the initiation of their own to do an idea!
This is a good thing to do with Python, the principle is simple, the code is very small, read first as fast.
The code is as follows:

Copy Code code 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/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 ()

In addition to the RFC documentation, this program can be slightly modified to do other things: such as bulk download MP3, E-books and so on.

Well, that's all, I hope to help you.

Related Article

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.