Coder Social home page Coder Social logo

scrapy-boilerplate's Introduction

scrapy-boilerplate

scrapy-boilerplate is a small set of utilities for Scrapy to simplify writing low-complexity spiders that are very common in small and one-off projects.

It requires Scrapy (>= 0.16) and has been tested using python 2.7. Additionally, PyQuery is required to run the scripts in the examples directory.

Note

The code is experimental, includes some magic under the hood and might be hard to debug. If you are new to Scrapy, don't use this code unless you are ready to debug errors that nobody have seen before.

Usage Guide

Items

Standard item definition:

from scrapy.item import Item, Field

class BaseItem(Item):
    url = Field()
    crawled = Field()

class UserItem(BaseItem):
    name = Field()
    about = Field()
    location = Field()

class StoryItem(BaseItem):
    title = Field()
    body = Field()
    user = Field()

Becomes:

from scrapy_boilerplate import NewItem

BaseItem = NewItem('url crawled')

UserItem = NewItem('name about location', base_cls=BaseItem)

StoryItem = NewItem('title body user', base_cls=BaseItem)

BaseSpider

Standard spider definition:

from scrapy.spider import BaseSpider

class MySpider(BaseSpider):
    name = 'my_spider'
    start_urls = ['http://example.com/latest']

    def parse(self, response):
        # do stuff

Becomes:

from scrapy_boilerplate import NewSpider

MySpider = NewSpider('my_spider')

@MySpider.scrape('http://example.com/latest')
def parse(spider, response):
    # do stuff

CrawlSpider

Standard crawl-spider definition:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor


class MySpider(CrawlSpider):
    name = 'my_spider'
    start_urls = ['http://example.com']

    rules = (
        Rule(SgmlLinkExtractor('category\.php'), follow=True),
        Rule(SgmlLinkExtractor('item\.php'), callback='parse_item'),
    )

    def parse_item(self, response):
        # do stuff

Becomes:

from scrapy_boilerplate import NewCrawlSpider

MySpider = NewCrawlSpider('my_spider')
MySpider.follow('category\.php')

@MySpider.rule('item\.php')
def parse_item(spider, response):
    # do stuff

Running Helpers

Single-spider running script:

# file: my-spider.py
# imports omitted ...

class MySpider(BaseSpider):
    # spider code ...

if __name__ == '__main__':
    from scrapy_boilerplate import run_spider
    custom_settings = {
        # ...
    }
    spider = MySpider()

    run_spider(spider, custom_settings)

Multi-spider script with standard crawl command line options:

# file: my-crawler.py
# imports omitted ...


class MySpider(BaseSpider):
    name = 'my_spider'
    # spider code ...


class OtherSpider(CrawlSpider):
    name = 'other_spider'
    # spider code ...


if __name__ == '__main__':
    from scrapy_boilerplate import run_crawler, SpiderManager
    custom_settings = {
        # ...
    }

    SpiderManager.register(MySpider)
    SpiderManager.register(OtherSpider)

    run_crawler(custom_settings)

Note

See the examples directory for working code examples.

scrapy-boilerplate's People

Contributors

rmax avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.