有一种方法可以在蜘蛛运行时阻止某些URL

时间:2019-07-26 18:53:23

标签: python python-3.x web-scraping scrapy scrapinghub

我正在使用scrapy框架编写蜘蛛(我正在使用抓取蜘蛛来抓取域中的每个链接)以从给定域中提取某些文件。我想阻止蜘蛛未找到文件的某些URL。例如,如果sider访问其中包含/ news /路径的URL一百次却找不到文件,我希望它停止在/ news /

中查找

我已经尝试过在找到self.rules变量时更新它,并且该路径不会产生文件,但这不起作用,并且它继续爬网具有该路径的URL

这是我要用来更新规则的功能

 def add_block_rule(self, match):
        new_rule = match.replace('/','\/')
        new_rule = f'/(.*{new_rule}.*)'
        if new_rule in self.deny_rules:
            return
        print(f'visted {match} to many times with out finding a file')
        self.deny_rules.append(new_rule)
        self.rules = (
            Rule( 
                LinkExtractor(
                    allow_domains=self.allowed_domains,
                    unique=True,
                    deny=self.deny_rules,),
                callback='parse_page',
                follow=True),
        )
        print(self.deny_rules)

我知道正在调用此函数,并且某些路径被访问了一百次而没有找到文件,但是没有使用新规则。我也知道正则表达式的工作原理与我尝试在 init 中定义一个正则表达式一样,它会阻止所需的路径。

我希望所有被访问超过100次而没有找到文件的路径都将被阻止并且不会被进一步访问

0 个答案:

没有答案