MongoDB plugins for Scrapy
Installation
pip install scrapy-mongo
Pipeline
This pipeline stores scraped items into a MongoDB collection.
Each item must have a unique id field to avoid duplicates.
This field is automatically mapped to MongoDB’s _id field.
Each item must include a collection field that specifies the name of the target MongoDB collection.
Items are upserted in batches of 100 by default.
The batch size can be adjusted using the PIPELINE_MONGO_BATCH_SIZE setting.
To enable the pipeline, include the following lines in settings.py:
ITEM_PIPELINES = {
'scrapy_mongo.MongoPipeline': 300,
}
PIPELINE_MONGO_URL = "mongodb://localhost:27017"
PIPELINE_MONGO_DATABASE = "mycollection"
Note: Update PIPELINE_MONGO_URL and PIPELINE_MONGO_DATABASE
with the appropriate values for the specific environment.
Cache
The cache component stores scraped responses in a MongoDB collection to avoid downloading the same pages multiple times. It leverages Scrapy’s fingerprinting mechanism to identify responses.
It uses Scrapy's fingerprint mechanism to identify the responses.
To enable caching, include the following lines in settings.py:
HTTPCACHE_ENABLED = True
HTTPCACHE_STORAGE = 'scrapy_mongo.MongoCacheStorage'
HTTPCACHE_MONGO_URL = "mongodb://localhost:27017"
HTTPCACHE_MONGO_DATABASE = "scraping"
HTTPCACHE_EXPIRATION_SECS = 604800 # Default is 1 week
Note: Update HTTPCACHE_MONGO_URL and HTTPCACHE_MONGO_DATABASE
with the appropriate values for the specific environment.
The default expiration time is set to 1 week (604800 seconds).
This value can be modified via HTTPCACHE_EXPIRATION_SECS.
Cache policy
An advanced cache policy mechanism with whitelist support is available. This feature allows for the definition of specific HTTP response codes to be cached, using both explicit lists and regular expressions.
To enable the cache policy, add the following lines to settings.py:
HTTPCACHE_ENABLED = True
HTTPCACHE_POLICY = 'scrapy_mongo.CacheOnlyPolicy'
HTTPCACHE_ACCEPT_HTTP_CODES = [302]
HTTPCACHE_ACCEPT_HTTP_CODES_REGEX = r'2\d\d'
This configuration will accept all 2XX HTTP codes and 302 redirects.
Error
The error component stores error logs in a MongoDB collection. It catches error from the Downloader pipeline and the Spider pipeline.
To enable error logging, include the following lines in settings.py:
DOWNLOADER_MIDDLEWARES = {
'scrapy_mongo.TraceErrorDownloaderMiddleware': 1000,
}
SPIDER_MIDDLEWARES = {
'scrapy_mongo.TraceErrorSpiderMiddleware': 1000,
}
ERROR_MONGO_URL = "mongodb://localhost:27017"
ERROR_MONGO_DATABASE = 'scraping'
ERROR_MONGO_COLLECTION = 'errors'
Note: Update ERROR_MONGO_URL, ERROR_MONGO_DATABASE and ERROR_MONGO_COLLECTION
with the appropriate values for the specific environment.
It is possible to use the same MongoDB connection for both the pipeline and cache
by replacing PIPELINE_MONGO_URL, HTTPCACHE_MONGO_URL and ERROR_MONGO_URL with a unified MONGO_URL setting.
Build for publish
Install dependencies:
pip install build twine
Build the package:
python -m build --outdir dist
And publish to PyPi:
python -m twine upload dist/*
Metadata
Release files for scrapy-mongo 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| scrapy_mongo-1.1.0.tar.gz | 5.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| scrapy_mongo-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.3 kB
Release files / scrapy_mongo-1.1.0.tar.gz
| Download URL | scrapy_mongo-1.1.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c83297e67eac122abc0cf68ead0d394c0b099cd0c25b2d5ca8c009ab427e9e06
|
|
BLAKE2b-256 checksum How to use checksums |
59964579ed7c3d74a07c70f4ab1341c4bc8b7e7f7ada4e4d24c5d99907dba221
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / scrapy_mongo-1.1.0-py3-none-any.whl
| Download URL | scrapy_mongo-1.1.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
826105f2f587574692c213bcd6c48f4bce33c381afa3b9c65d69954ed979c378
|
|
BLAKE2b-256 checksum How to use checksums |
7a5348ad4eb70a0bf8384c2e8b325a2b20f9d46619065e2b77a1d35df3803057
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for scrapy-mongo 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.