[Python] Crawl embarrassing encyclopedia

Source: Internet
Author: User

Tag:python    crawler    

# coding=utf-8import Urllib2import urllibimport reclass qiushi:def _init_ (self): self.page = 1 # Get embarrassing from web page def G Etqiushis (self,page): #网址 url = "http://www.qiushibaike.com/hot/page/" +page #伪装浏览器 user_agent = ' mozilla/4.0 ( Compatible MSIE 5.5; Windows NT) ' headers = {' User-agent ': user_agent} #请求 req = Urllib2. Request (url,headers = headers) Response = Urllib2.urlopen (req) HTML = response.read () #encode的作用是将unicode编码 Convert to another encoded string #decode的作用是将其他编码的字符串转换成unicode编码 unicodehtml = Html.decode ("utf-8") items = Re.findall (' &L t;div.*?class= "Content". *?title= "(. *?)" > (. *?) </div> ', Unicodehtml,re. S) contents = [] for item in items: # The first of the item is the title of the Div, that is, the second of the time # item is the content of the Div, which is the inner     Capacity Contents.append ([Item[0].replace ("\ n", ""), Item[1].replace ("\ n", "")]) return contents #打印糗事 def Showqiushi (self,contents): Count = 1 for content in Contents:print "Article%d embarrassing thing "% count,content[0]," \ n "Print content[1]," \ n "Count + = 1 #启动 def Start (self): page = 1 The while page < 5:print "page%d: \ n"% page contents = self. Getqiushis (str (page)) self.       Showqiushi (contents) page + = 1 Qiushi = Qiushi () Qiushi.start ()





[Python] Crawl embarrassing encyclopedia

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.