Get a 51CTO blog list in reverse chronological order using Python

Source: Internet
Author: User

I saw this shell interview question before: get a list of 51CTO blogs sorted in reverse chronological order http://oldboy.blog.51cto.com/2561410/1860985

Since I've been learning python for a while, think about using Python to solve problems.


Idea: Through the requests module to obtain the Web page source code, through the split () function to get the total number of pages, stitching the string to get all the URL of the blog address. Similarly, the source code of all the pages is obtained through requests and the key information is obtained through regular matching, thus stitching out the HTML page.


#coding: Utf-8import requestsimport re,sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') #  get page def  page ():    url =  ' http://oldboy.blog.51cto.com/all/2561410 '      content = requests.get (URL). Content    msg = re.findall (R ' <center> (. *) </center> ', content) [0]    return msg.split ('/') [ -1].split () [0 ]#  get key information and Stitch Htmldef oldboy ():    uri =  ' http://oldboy.blog.51cto.com/ all/2561410/page/'     num = page ()     res = []     for i in range (num):        &NBSP;URL&NBSP;=&NBSP;URI&NBSP;+&NBSP;STR (int (i) +1)      content =  Requests.get (URL). Content    res += re.findall (R ' <a href= "(/\d+/\d+)" > (. *) </a> ', content) &NBSP;&NBSp;  # res format [('/2561410/1867160 ', ' 2017\XD7\XEE\XD0\XC2\XC6\XF3\XD2\XB5SHELL\XC3\XE6\XCA\XD4\XCC \xe2\xbc\xb0\xc6\xf3\xd2\xb5\xd4\xcb\xce\xac\xca\xb5\xd5\xbd\xb9\xb230\xb5\xc0\xb0\xb8\xc0\xfd '),...]     html =  '     for i in res:     msg = i[1].decode (' GBK ')     href =  ' HTTP// Oldboy.blog.51cto.com ' +i[0]    html +=  ' <p><br></p><p>{0 }</p><p><a href= ' {1} '  target= ' _blank ' >{1}</a></p> '. Format (MSG,HREF)         # html format "<p><br></p><p> 2017 the latest enterprise Shell face test and enterprise operation and maintenance 30 cases </p><p><a href= ' http://oldboy.blog.51cto.com/2561410/1867160 '  target= ' _blank ' >http://oldboy.blog.51cto.com/2561410/1867160</a></p&gt, ... "     with open (' oldboy_blog.html ', ' W ')   as f:        f.write (HTML) if __name__== ' __main__ ':     oldboy ()


HTML page

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/89/C2/wKioL1gcAVmTchDtAAAyUb0_Jo0894.png "title=" Qq20161104113227.png "alt=" Wkiol1gcavmtchdtaaayub0_jo0894.png "/>


Get a 51CTO blog list in reverse chronological order using Python

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.