Python Making embarrassing encyclopedia crawler

Source: Internet
Author: User
Morning up idle to do nothing, inexplicably pop up embarrassing things Encyclopedia of the jokes, then a think since you come to, then I write a crawler to your site crawl, one as practice practiced hand, and second also count for some fun.

In fact, these two days are also in touch with the contents of the database, you can keep the crawled data in the database for later use. All right, don't say a word, let's take a look at the data results of the program crawl

It is worth mentioning that in the program I want to crawl the content of the encyclopedia 30 page, but there is a connection error, when I put the page down to 20 pages, the program can run normally, do not know what the reason, eager to know the great God can tell me, grateful.

The program is very simple, directly on the source code slightly

# coding=utf8import Reimport requestsfrom lxml import etreefrom multiprocessing.dummy import Pool as Threadpoolimport SYSR Eload (SYS) sys.setdefaultencoding (' Utf-8 ') def getnewpage (URL, total): nowpage = Int (Re.search (' (\d+) ', URL, re. S). Group (1)) URLs = [] for I in range (nowpage, total + 1): link = re.sub (' (\d+) ', '%s '% i, URL, re. S) urls.append (link) return urlsdef spider (URL): html = requests.get (URL) selector = etree. HTML (html.text) Author = Selector.xpath ('//*[@id = "Content-left"]/p/p[1]/a[2]/@title ') content = Selector.xpath ('//*[ @id = "Content-left"]/p/p[2]/text () ') vote = Selector.xpath ('//*[@id = "Content-left"]/p/p[3]/span/i/text () ') length = Len (author) for I in range (0, length): F.writelines (' Author: ' + author[i] + ' \ n ') f.writelines (' content: ' + str (content[i]). Re  Place (' \ n ', ') + ' \ n ') F.writelines (' Support: ' + vote[i] + ' \ n ') if __name__ = = ' __main__ ': F = open (' Info.txt ', ' a ') URL = ' http://www.qiushibaike.com/text/page/1/' urls = getnewpage (URL, a) pool = ThreadPool (4) Pool.map (Spider,urls) F.close () 

If you don't know the part, you can refer to my top three articles in turn.

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.