2024-04-29 05:40:42 发布
网友
我正在用Scrapy爬网成千上万的网站。我有一个大的域名列表要抓取。一切都很好,只是爬虫也遵循外部链接,这就是为什么它爬行的领域太多,超出了必要的范围。我已经尝试过在SGMLlinkextractor中使用“allow_domains”,但是当我解析一个巨大的域列表时,这不起作用。在
所以我的问题是:我如何才能限制一个广泛的爬行到内部链接?在
有什么好主意吗。在
更新:该问题是由allow_domains列表引起的,该列表太大,无法处理scrapy
我可以通过修改SGMLlinkextractor来解决这个问题。在返回链接之前,我添加了以下两行:
domain = response.url.replace("http://","").replace("https://","").split("/")[0] links = [k for k in links if domain in k.url]
OffsiteMiddleware是您应该考虑使用的:
class scrapy.contrib.spidermiddleware.offsite.OffsiteMiddlewareFilters out Requests for URLs outside the domains covered by the spider.This middleware filters out every request whose host names aren’t in the spider’s allowed_domains attribute.
class scrapy.contrib.spidermiddleware.offsite.OffsiteMiddleware
Filters out Requests for URLs outside the domains covered by the spider.
This middleware filters out every request whose host names aren’t in the spider’s allowed_domains attribute.
我可以通过修改SGMLlinkextractor来解决这个问题。在返回链接之前,我添加了以下两行:
OffsiteMiddleware是您应该考虑使用的:
相关问题 更多 >
编程相关推荐