Python crawler A second program

Source: Internet
Author: User

#!/usr/bin/python#encoding=utf-8import urllib2    import urllib     import re    import thread    import time     class spider (object):        def   __init__ (self):        self.page = 1         self.pages = []         Self.enable = false        def start (self):         self.enable = True         page = self.page        thread.start_new_thread (self. LoadPage, ())          while self.enable:             if self.pages:                 nowpage = self.pages[0]                 del self.pages[0]                 self. ShowPage (nowpage,page)                  page += 1                    def loadpage (self):         While self.enable:            if len ( Self.pages) <2:                 try:           &Nbsp;        mypage = self. GetPage (str (self.page))                      self.pages.append (MyPage)                      self.page += 1                 except:                     print   "Unable to link embarrassing encyclopedia"             else:                 time.sleep (1)                           def showpage (self,nowpage,page):        for item in nowpage:             print  "the %d page"% (page)              print item[0],item[1]             myinput = raw_input ()              if myInput ==  "Q"  :                 self.enable = False                 break                     def  getpage (self,page):        url =  "/http m.qiushibaike.com/hot/page/" + page         user_agent =  ' Mozilla/4.0  (compatible; &NBSP;MSIE&NBSP;5.5;&NBSP;WINDOWS&NBSP;NT) '         headers =  {"User-agent": User_agent}        req = urllib2. Request (url,headers = headers)         res =  Urllib2.urlopen (req)         mypage = res.read ()          unicodepage = mypage.decode ("Utf-8")                  #  Find all class= "content" div tags               #re. S is any matching pattern, that is. Can match line break             myitems =  re.findall          (' <div.*? class= "Content". *?title= "(. *?)" > (. *?) </div> ', Unicodepage,re. S)           items = []         for i in myItems:             items.append ([i[0],i[1]])         return items#- Entrance to----------  program  -----------    print u "" "           ---------------------------------------                Program: Embarrassing reptile                 language:python 2.7                operation: Enter q             quit reading embarrassing encyclopedia                function: Press ENTER to browse today's embarrassing hot           ------------ ---------------------------           ""                               print u ' Please press ENTER to view today's embarrassing content: '     raw_input ('   ')        my_spider = spider () My_spider. Start ()


Python crawler A second program

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.