Python crawler crawls a Han-han Sina Blog

Source: Internet
Author: User



Online to see the great god to Python crawler crawling to very much useful information, think very powerful. Suddenly want to learn Python crawler, although he did not learn python. But on the internet to look for some information to see the climb to the Han-Han Sina blog video. A total of three episodes, the first section to crawl a blog, the second section to crawl a page of blog. The third episode tells you to crawl all blogs.

Watched the video. Also left the code.

Crawler First step: Check the source of the Web page:

The code for the first blog is blue-bottomed section <a title= "" target= "_blank" href= "Http://blog.sina.com.cn/s/blog_ 4701280b0102eo83.html ">" on the seven elements of film--about my ...</a> on electricity

Find the Common section "< title=" ' href= ', '. html ', against the code of other blogs


The code is:

#-*-Coding:-utf-8-*-import urllibstr0 = ' <a title= ' "target=" _blank "href=" http://blog.sina.com.cn/s/blog_ 4701280b0102eo83.html ">" on the seven elements of film--About me ...</a> ' title = Str0.find (R ' <a title ') #print Titlehref = Str0.find (R ' href= ') #print hrefhtml = Str0.find (R '. html ') #print Htmlurl = str0[href + 6:html + 5]print urlcontent = Urllib.urlopen (u RL). Read () #print contentfilename = Url[-26:]print filenameopen (filename+ '. html ', ' W '). Write (content) print ' Download successful! '

Execution Result:




saved files:


Python crawler crawls a Han-han Sina Blog

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.