Python-implemented bulk download of RFC documents

Source: Internet
Author: User
There are a lot of RFC documents, and sometimes you want to flip through without the Internet, you can only download one copy locally.
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 thunderbolt ...
When available, it only supports three-digit expansion (with Thunderbolt 7), and I want exactly four digits ...
Depressed under the initiation of their own to do an idea!
This thing is very suitable for python, the principle is very simple, the code is very small, first read as fast.
The code is as follows:
Copy the 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, this program can do other things with a little modification: such as bulk download MP3, ebooks, etc.

All right, that's it, I hope it helps 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.