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:
sleep(2) # Simulate a delay for processing
print(">>>", item, "<<<")
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"]) # Start scraping
How it works:
- Define a Spider: In this example,
MySpiderextracts the title of a webpage. - Define a Processor:
MyProcessorprocesses scraped items (here it simply sleeps for 2 seconds to simulate real processing). - Run the ScrapyRunner: The
ScrapyRunnerclass is used to run the spider and process the items. Therun()method triggers the scraping, and each item scraped is passed to the custom processor.
Customization
Custom Processor
To create your own custom processor:
- Subclass
ItemProcessor. - Override the
process_item()method to handle scraped items. - 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:
settings = {
"LOG_LEVEL": "DEBUG",
"USER_AGENT": "MyCustomAgent",
# Add more custom settings as needed
}
runner = ScrapyRunner(spider=MySpider, settings=settings)
runner.run(start_urls=["https://example.org"])
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scrapyrunner-0.0.1.tar.gz.
File metadata
- Download URL: scrapyrunner-0.0.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.0 CPython/3.13.1 Darwin/22.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e91b8a3fb5fb21caa1658ddbf55e00e48225ff0f14c60dfe98c30f7192a3fa73
|
|
| MD5 |
6d779526d916d1bd204389cefe382607
|
|
| BLAKE2b-256 |
66e246455501f1aa04bdd804a547d6e4ed4b42cb711c73b6c147f85943619eb3
|
File details
Details for the file scrapyrunner-0.0.1-py3-none-any.whl.
File metadata
- Download URL: scrapyrunner-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.5 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e900ea8eeec8c3856bdd8653b47b1bfc0097d84e6dfc81d77bd7d636e1244098
|
|
| MD5 |
270bbd68a4ce071e21f10bb0581b7a69
|
|
| BLAKE2b-256 |
8ccbd7eab3846c819678e23710ab3b60877cee9b177df92b43d9e3976ce9c97c
|