Scrapy-link-filter
Spider Middleware that allows a Scrapy Spider to filter requests. There is similar functionality in the CrawlSpider already using Rules and in the RobotsTxtMiddleware, but there are twists. This middleware allows defining rules dinamically per request, or as spider arguments instead of project settings.
Install
This project requires Python 3.6+ and pip. Using a virtual environment is strongly encouraged.
$ pip install git+https://github.com/croqaz/scrapy-link-filter
Usage
For the middleware to be enabled as a Spider Middleware, it must be added in the project settings.py:
SPIDER_MIDDLEWARES = {
# maybe other Spider Middlewares ...
# can go after DepthMiddleware: 900
'scrapy_link_filter.middleware.LinkFilterMiddleware': 950,
}
Or, it can be enabled as a Downloader Middleware, in the project settings.py:
DOWNLOADER_MIDDLEWARES = {
# maybe other Downloader Middlewares ...
# can go before RobotsTxtMiddleware: 100
'scrapy_link_filter.middleware.LinkFilterMiddleware': 50,
}
The rules must be defined either in the spider instance, in a spider.extract_rules dict, or per request, in request.meta['extract_rules'].
Internally, the extract_rules dict is converted into a LinkExtractor, which is used to match the requests.
Note that the URL matching is case-sensitive by default, which works in most cases. To enable case-insensitive matching, you can specify a "(?i)" inline flag in the beggining of each "allow", or "deny" rule that needs to be case-insensitive.
Example of a specific allow filter, on a spider instance:
from scrapy.spiders import Spider
class MySpider(Spider):
extract_rules = {"allow_domains": "example.com", "allow": "/en/items/"}
Or a specific deny filter, inside a request meta:
request.meta['extract_rules'] = {
"deny_domains": ["whatever.com", "ignore.me"],
"deny": ["/privacy-policy/?$", "/about-?(us)?$"]
}
The possible fields are:
allow_domainsanddeny_domains- one, or more domains to specifically limit to, or specifically rejectallowanddeny- one, or more sub-strings, or patterns to specifically allow, or reject
All fields can be defined as string, list, set, or tuple.
License
BSD3 © Cristi Constantin.
Metadata
Release files for scrapy-link-filter 0.2.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 | Actions |
|---|---|---|---|---|
| scrapy-link-filter-0.2.0.tar.gz | 5.4 kB | Without provenance | Details for scrapy-link-filter-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scrapy_link_filter-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for scrapy_link_filter-0.2.0-py3-none-any.whl |
Total release size: 11.7 kB
Release files / scrapy-link-filter-0.2.0.tar.gz
| Download URL | scrapy-link-filter-0.2.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64bf701cbbbc9f51dad47094c1effbf9ca47b6d6e9f54c0f64cedefcbbbc72e4
|
|
BLAKE2b-256 checksum How to use checksums |
3d1c175ecef969380c969abbd90f1063b8375fe68131f8a2c0c5995beb0b0a84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.23.4 CPython/3.6.9
|
Release files / scrapy_link_filter-0.2.0-py3-none-any.whl
| Download URL | scrapy_link_filter-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1f6c25569a765945a331daac3929731cd1f4e07ad129705ee53d0b6e65f9d36
|
|
BLAKE2b-256 checksum How to use checksums |
a8d549b7e0fcd23809a513a59d9224812355d2b8424b672b3963e7d8155e4ba9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.23.4 CPython/3.6.9
|
Security for scrapy-link-filter 0.2.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.