【发布时间】:2020-02-21 10:30:37
【问题描述】:
我正在尝试实现与 ScreamingFrog 相同的功能——测量 url 深度。
为此,我正在访问 response.meta 的深度参数,就像这样:response.meta.get('depth', 0),但我得到的结果与 ScreamingFrog 的结果有很大不同。
因此,我想通过保存CrawlSpider 所经过的所有页面来调试为什么会发生这种情况,以便到达当前页面。
这是我当前的蜘蛛的样子:
class FrSpider(scrapy.spiders.CrawlSpider):
"""Designed to crawl french version of dior.com"""
name = 'Fr'
allowed_domains = [website]
denyList = []
start_urls = ['https://www.%s/' % website]
rules = (Rule(LinkExtractor(deny=denyList), follow=True, callback='processLink'),)
def processLink(self, response):
link = response.url
depth = response.meta.get('depth', 0)
print('%s: depth is %s' % (link, depth))
这里比较我的爬虫和screamingfrog之间的爬取统计数据(同一网站,仅限于前500页):
Depth(Clicks from Start Url) Number of Urls % of Total
1 62 12.4
2 72 14.4
3 97 19.4
4 49 9.8
5 40 8.0
6 28 5.6
7 46 9.2
8 50 10.0
9 56 11.2
---------------------------- -------------- ----------
对比
正如您所看到的,它有很大的不同,通过将抓取从前 500 个页面扩展到整个网站,这两种方法之间存在巨大差异。
我想知道是否有人可以向我指出我正在做的错误,或者帮助我提供有关如何存储爬虫通过的所有页面以到达当前页面的建议。 可视化如下所示:
【问题讨论】:
标签: python python-3.x web-scraping scrapy