Skip to main content

Handy tools to maintain persistent meta values between requests in Scrapy spiders

Project description

Handy tools to maintain persistent meta values between requests in Scrapy spiders. Available as spider middleware and spider callback decorators.

Installation

pip install stickymeta

Usage

As spider middleware

Add middleware to settings.py:

SPIDER_MIDDLEWARES = {
    ...
    'stickymeta.StickyMetaMiddleware': 543,
    ...
}

and sticky_meta attribute containing persistent meta keys to spider:

class TheSpider(scrapy.Spider):
    name = 'thespider'
    sticky_meta = ('cookiejar', 'foo', 'bar')

All values for the corresponding keys will be kept persistent between all the requests and responses.

As decorators

@stick_meta

from stickymeta import stick_meta

Keep persistent values for keys passed as decorator parameters:

@stick_meta('a', 'b', 'c')
def parse(self, response):
    ...
    yield scrapy.Request(url)

is equivalent to:

def parse(self, response)
    ...
    meta = {
        'a': response.meta['a'],
        'b': response.meta['b'],
        'c': response.meta['c'],
    }
    yield scrapy.Request(url, meta=meta}

@stick_cj

from stickymeta import stick_cj

Shortcut for stick_meta handling cookiejar as default argument value, so

@stick_cj('a', 'b', 'c')
def parse(self,response):
    ...

is equivalent to

@stick_meta('cookiejar', 'a', 'b', 'c')
def parse(self,response):
    ...

Spider attribute sticky_meta also affects to decorators, next two pieces of code will handle meta in the same way:

class TheSpider(scrapy.Spider):
    name = 'thespider'
    sticky_meta = ('a', 'b', 'c')

    @stick_meta()
    def parse(self, response):
        ...
        yield Request(url)

vs

class TheSpider(scrapy.Spider):
    name = 'thespider'

    @stick_meta('a', 'b', 'c')
    def parse(self, response):
        ...
        yield Request(url)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stickymeta-0.0.5.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stickymeta-0.0.5-py2-none-any.whl (6.0 kB view details)

Uploaded Python 2

File details

Details for the file stickymeta-0.0.5.tar.gz.

File metadata

  • Download URL: stickymeta-0.0.5.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for stickymeta-0.0.5.tar.gz
Algorithm Hash digest
SHA256 505ca9d85cdf7b2aeccc06762d42584898d104b70a16927686e095380c7aa361
MD5 d3589b5cc88b3fe79d020bd789eead7a
BLAKE2b-256 f275ec63f8f6d432f686342d21f87c8db4060476162251123fc1c277d58c5df2

See more details on using hashes here.

File details

Details for the file stickymeta-0.0.5-py2-none-any.whl.

File metadata

File hashes

Hashes for stickymeta-0.0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 5edda9545bf4b15ad93d1879b4c3ebd194d504810df2c9e132a428b69e2133cc
MD5 91efb3401fba630bd107fa169c214f11
BLAKE2b-256 6fad5294c09bebba39ce581920d2b7a2b3fd589abe148274191a4b054fe7dde4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page