Skip to main content

A Python library to run Scrapy spiders directly from your code.

Project description

ScrapyRunner

A Python library to run Scrapy spiders directly from your code.

Overview

ScrapyRunner is a lightweight library that enables you to run Scrapy spiders in your Python code, process scraped items using custom processors, and manage Scrapy signals seamlessly. It simplifies the process of starting and managing Scrapy spiders and integrates well with your existing Python workflows.

Features

  • Run Scrapy spiders directly from Python code.
  • Process scraped items in batches with a custom processor.
  • Manage Scrapy signals (e.g., on item scraped, on engine stopped).
  • Easy integration with the Scrapy framework.
  • Asynchronous processing of items using Twisted.

Installation

To install ScrapyRunner, you can use pip:

pip install scrapyrunner

Usage

Example

from time import sleep
import scrapy
from scrapyrunner import ScrapyRunner, ItemProcessor

# Define a Scrapy Spider
class MySpider(scrapy.Spider):
    name = 'example'
    
    def parse(self, response):
        data = response.xpath("//title/text()").extract_first()
        return {"title": data}

# Define a custom Item Processor
class MyProcessor(ItemProcessor):
    def process_item(self, item: scrapy.Item) -> None:
        print(">>>", item, "<<<")
        sleep(2)  # Simulate a delay for processing

if __name__ == '__main__':
    # Create an instance of ScrapyRunner with the spider and processor
    scrapy_runner = ScrapyRunner(spider=MySpider, processor=MyProcessor)
    scrapy_runner.run(start_urls=["https://example.org", "https://scrapy.org"])  # Start scraping

How it works:

  1. Define a Spider: In this example, MySpider extracts the title of a webpage.
  2. Define a Processor: MyProcessor processes scraped items (here it simply sleeps for 2 seconds to simulate real processing).
  3. Run the ScrapyRunner: The ScrapyRunner class is used to run the spider and process the items. The run() method triggers the scraping, and each item scraped is passed to the custom processor.

Customization

Custom Processor

To create your own custom processor:

  1. Subclass ItemProcessor.
  2. Override the process_item() method to handle scraped items.
  3. Process each item as needed (e.g., save to a database, perform additional transformations, etc.).
class MyCustomProcessor(ItemProcessor):
    def process_item(self, item: scrapy.Item) -> None:
        # Custom processing logic goes here
        print("Processing item:", item)

Custom Settings

You can pass custom Scrapy settings to ScrapyRunner:

scrapy_settings = {
    "LOG_LEVEL": "DEBUG",
    "USER_AGENT": "MyCustomAgent",
    # Add more custom settings as needed
}

runner = ScrapyRunner(spider=MySpider, processor=MyProcessor, scrapy_settings=scrapy_settings)
runner.run(start_urls=["https://example.org", "https://scrapy.org"])

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

scrapyrunner-0.0.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

scrapyrunner-0.0.4-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file scrapyrunner-0.0.4.tar.gz.

File metadata

  • Download URL: scrapyrunner-0.0.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.1 Darwin/22.6.0

File hashes

Hashes for scrapyrunner-0.0.4.tar.gz
Algorithm Hash digest
SHA256 b9c246a6c9f6930ccba459831a2f1ece9cff47151e3133b8b6f79b15b5503d1e
MD5 413ba1e4b76c7f697df985d60187fc1e
BLAKE2b-256 da64889b1f2d7d10eef70f662bf162f868feca9cca322732f00acc584655d547

See more details on using hashes here.

File details

Details for the file scrapyrunner-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: scrapyrunner-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.1 Darwin/22.6.0

File hashes

Hashes for scrapyrunner-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2f614ac72e8d6d898be95fd94064e62a6fa3732f451f38ab125883ba0ffa1375
MD5 897a759f4df5caac94b539c2d9ad2216
BLAKE2b-256 11b00778dc44e836ac89c774326671d5eb714d16645923db414ee88902375ab4

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