【问题标题】:scrapy deny rules not being ignored不被忽略的scrapy拒绝规则
【发布时间】:2012-01-09 20:24:19
【问题描述】:

我有一些从数据库中动态获取的规则并将它们添加到我的蜘蛛中:

        self.name =  exSettings['site']
        self.allowed_domains = [exSettings['root']]
        self.start_urls = ['http://' + exSettings['root']]

        self.rules =  [Rule(SgmlLinkExtractor(allow=(exSettings['root'] + '$',)), follow= True)]
        denyRules = []

        for rule in exSettings['settings']:
            linkRegex = rule['link_regex']

            if rule['link_type'] == 'property_url':
                propertyRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True, callback='parseProperty')
                self.rules.insert(0, propertyRule)
                self.listingEx.append({'link_regex': linkRegex, 'extraction': rule['extraction']})

            elif rule['link_type'] == 'project_url':
                projectRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True) #not set to crawl yet due to conflict if same links appear for both
                self.rules.insert(0, projectRule)

            elif rule['link_type'] == 'favorable_url':
                favorableRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True)
                self.rules.append(favorableRule)

            elif rule['link_type'] == 'ignore_url':
                denyRules.append(linkRegex)

        #somehow all urls will get ignored if allow is empty and put as the first rule
        d = Rule(SgmlLinkExtractor(allow=('testingonly',), deny=tuple(denyRules)), follow=True)

        #self.rules.insert(0,d) #I have tried with both status but same results
        self.rules.append(d)

我的数据库中有以下规则:

link_regex: /listing/\d+/.+  (property_url)
link_regex: /project-listings/.+    (favorable_url)
link_regex: singapore-property-listing/   (favorable_url)
link_regex: /mrt/  (ignore_url)

我在日志中看到了这一点:

 http://www.propertyguru.com.sg/singapore-property-listing/property-for-sale/mrt/125/ang-mo-kio-mrt-station> (referer: http://www.propertyguru.com.sg/listing/8277630/for-sale-thomson-grand-6-star-development-)

/mrt/ 不应该被拒绝吗?为什么我上面的链接还是爬到了?

【问题讨论】:

    标签: python web-crawler scrapy


    【解决方案1】:

    据我所知,deny 参数必须在同一个 SgmlLinkExtractor 中,后者具有 allow 模式。

    在您的情况下,您创建了SgmlLinkExtractor,它允许favorable_url ('singapore-property-listing/')。但是这个提取器没有任何deny 模式,所以它也提取了/mrt/

    要解决此问题,您应该将deny 模式添加到通讯员SgmlLinkExtractors。另请参阅related question

    也许有一些方法可以定义全局deny 模式,但我还没有见过。

    【讨论】:

    • 是的,你是对的。在查看源之后,拒绝将简单地跳过匹配的链接,但它仍会将跳过的链接传递给后续规则中的提取器。
    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多