A simple spider collection program based on scrapy implementation

Source: Internet
Author: User
In this paper, a simple spider collection program based on scrapy implementation is described. Share to everyone for your reference. Specific as follows:

# Standard Python Library imports# 3rd party importsfrom scrapy.contrib.spiders import Crawlspider, Rulefrom Scrapy.contri B.LINKEXTRACTORS.SGML Import sgmllinkextractorfrom scrapy.selector import htmlxpathselector# My importsfrom Poetry_ Analysis.items Import poetryanalysisitemhtml_file_name = R '. +\.html ' class Poetryparser (object): "" "provides common par  Sing method for poems formatted this one specific. "" "Date_pattern = R ' (\d{2} \w{3,9} \d{4}) ' Def parse_poem (self, response): HxS = htmlxpathselector (response) ITE m = Poetryanalysisitem () # All poetry text was in pre tags text = hxs.select ('//pre/text () '). Extract () item[' text ' ] = '. Join (text) item[' url '] = Response.url # Head/title contains title-a poem by author Title_text = Hxs.sele CT ('//head/title/text () '). Extract () [0] item[' title ', item[' author '] = title_text.split ('-') item[' author '] = Item [' Author '].replace (' A poem by ', ") for key in [' title ', ' Author ']: Item[key] = Item[key].Strip () item[' Date ' = Hxs.select ("//p[@class = ' small ']/text ()"). Re (Date_pattern) return Itemclass Poetryspider (Crawl   Spider, poetryparser): name = ' example.com_poetry ' allowed_domains = [' www.example.com '] root_path = ' someuser/poetry/' Start_urls = [' http://www.example.com/someuser/poetry/recent/', ' Http://www.example.com/someuser/poetry/less_rec       ent/'] rules = [Rule (Sgmllinkextractor (allow=[start_urls[0] + html_file_name]), callback= ' Parse_poem '), Rule (Sgmllinkextractor (allow=[start_urls[1] + html_file_name]), callback= ' Parse_poem ')

Hopefully this article will help you with Python programming.

  • 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.