Batch download of RFC documentation (Python implementation)

Source: Internet
Author: User

There are many RFC documents. Sometimes I want to read them without being connected to the Internet. 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 python. The principle is very simple,CodeAnd very few. Read it fast first.
The Code is as follows:

 1   #  ! /Usr/bin/Python  
2 '''
3 File: getrfc. py
4 Author: Mike
5 E-mail: Mike_Zhang@live.com
6 '''
7 Import Urllib, OS, shutil, time
8
9 Def Downloadhtmlpage (URL, tmpf = '' ):
10 I = URL. rfind ( ' / ' )
11 Filename = URL [I + 1:]
12 If Tmpf: filename = tmpf
13 Print URL, " -> " , Filename
14 Urllib. urlretrieve (URL, filename)
15 Print ' Downloaded ' , Filename
16 Time. Sleep (0.2)
17 Return Filename
18
19 # Http://www.networksorcery.com/enp/rfc/rfc1000.txt
20 # Http://www.networksorcery.com/enp/rfc/rfc6409.txt
21 If _ Name __ = ' _ Main __ ' :
22 ADDR = ' Http://www.networksorcery.com/enp/rfc '
23 Dirpath = " RFC "
24 # Startindex= 1000
25 Startindex = int (raw_input ( ' Start: ' ))
26 # Endindex = 6409
27 Endindex = int (raw_input (' End: ' ))
28 If Startindex> endindex:
29 Print ' Input error! '
30 If False = OS. Path. exists (dirpath ):
31 OS. makedirs (dirpath)
32 Filedownloadlist = []
33 Logfile = open ( " Log.txt " , " W " )
34 For I In Range (startindex, endindex + 1 ):
35 Try :
36 T_url = ' % S/rfc0000d.txt ' % (ADDR, I)
37 Filename = downloadhtmlpage (t_url)
38 Oldname = ' ./ ' + Filename
39 Newname = ' ./ ' + Dirpath +' / ' + Filename
40 If True = OS. Path. exists (oldname ):
41 Shutil. Move (oldname, newname)
42 Print ' Moved ' , Oldname, ' To ' , Newname
43 Except :
44 Msglog = ' Get % s failed! ' % (I)
45 Print Msglog
46 Logfile. Write (msglog + ' \ N ' )
47 Continue
48 Logfile. Close ()

In addition to the RFC documentationProgramYou can also do other things with a slight modification, such as batch downloading MP3 files and e-books.

Okay, that's all. I hope it will 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.