You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Scrapy CrawlSpider中配置URL拒绝规则?

Fixing Scrapy LinkExtractor to Exclude /products, /collections Paths

Got it, let's get your Scrapy spider to stop following those unwanted /products and /collections URLs. The issue with your current setup is that you're not telling the LinkExtractor to reject those paths—right now it's pulling in all links from your allowed domains, which is why those URLs are still being processed.

Here's how to fix it, step by step:

1. Add the deny Parameter to LinkExtractor

The LinkExtractor has a built-in deny parameter that lets you specify regex patterns for URLs you want to exclude. Update your rules like this:

rules = (
    Rule(
        LinkExtractor(
            allow_domains=allowed_domains,
            deny=[r'/products/', r'/collections/']  # Reject URLs containing these paths
        ),
        callback='parse_page',
        process_links='process_links',
        follow=True
    ),
)

A quick note on the regex:

  • r'/products/' will match any URL that contains this substring, so it catches things like https://example.com/products/123, https://example.com/shop/products, etc.
  • If you need stricter matching (e.g., only URLs starting with /products), use r'^/products/' instead. Adjust based on your specific needs.

If you still see those unwanted URLs being processed after adding the deny rules, double-check your process_links function. It’s possible that function is modifying the links and reintroducing the URLs you tried to exclude.

Make sure process_links isn’t adding back links that match the /products or /collections patterns. For example, if you’re filtering links in that function, ensure it’s complementing the deny rules instead of overriding them.

3. Verify with Debug Logs

To confirm the rules are working, enable Scrapy’s debug logs by adding this to your settings:

LOG_LEVEL = 'DEBUG'

Then run your spider and look for log lines like:

Filtered link denied by deny rules:

This confirms the LinkExtractor is correctly excluding those paths.


内容的提问来源于stack exchange,提问作者Huzaifa Farooq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:22:31