【问题标题】:Scrapy crawl only internal links, including relative linksScrapy只抓取内部链接,包括相对链接
【发布时间】:2016-11-04 13:02:42
【问题描述】:

我需要使用 scrapy 抓取页面的所有内部网络链接,例如 www.stackovflow.com 上的所有链接都会被抓取。这段代码的工作:

   extractor = LinkExtractor(allow_domains=self.getBase(self.startDomain))

    for link in extractor.extract_links(response):
        self.registerUrl(link.url)

但是有一个小问题,所有相对路径(例如 /meta/questions/ask)都不会被抓取,因为它们不包含基域 stackoverflow.com。任何想法如何解决这一问题?

【问题讨论】:

标签: python scrapy


【解决方案1】:

如果我理解正确的问题,你想使用 scrapy.spidermiddlewares.offsite.OffsiteMiddleware https://doc.scrapy.org/en/latest/topics/spider-middleware.html#scrapy.spidermiddlewares.offsite.OffsiteMiddleware

过滤掉对所覆盖域之外的 URL 的请求 蜘蛛。

This middleware filters out every request whose host names aren’t in the spider’s allowed_domains attribute. All subdomains of any

列表中的域也是允许的。例如。规则 www.example.org 也将允许 bob.www.example.org 但不允许 www2.example.com 也不 example.com。

When your spider returns a request for a domain not belonging to those covered by the spider, this middleware will log a debug message

类似这个:

DEBUG: Filtered offsite request to 'www.othersite.com': <GET http://www.othersite.com/some/page.html>

To avoid filling the log with too much noise, it will only print one of these messages for each new domain filtered. So, for example,

如果对 www.othersite.com 的另一个请求被过滤,则没有日志消息 将被打印。但是,如果对 someothersite.com 的请求被过滤,则 将打印消息(但仅针对过滤的第一个请求)。

If the spider doesn’t define an allowed_domains attribute, or the attribute is empty, the offsite middleware will allow all requests.

If the request has the dont_filter attribute set, the offsite middleware will allow the request even if its domain is not listed in

允许的域。

我的理解是 URL 在被过滤之前已经过规范化。

【讨论】:

  • 是否应在 settings.py 中将 OffsiteMiddleware 设置为禁用?
  • 没有'scrapy.spidermiddlewares.offsite.OffsiteMiddleware':500,见doc.scrapy.org/en/latest/topics/…
猜你喜欢
  • 2014-09-10
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多