Scrapy CrawlSpider-start_urls에 대한 errback

Sep 23 2020

나는 CrawlSpider규칙 linkextractor와 함께 사용 하고 있습니다 errback.

나는 parse_start_url구문 분석을 할 수 있도록 사용 하고 start_urls있지만 errback그들도 필요 합니다.

class CS(CrawlSpider):
    name = "CS"
    rules = (Rule(LinkExtractor(allow=[], deny=[]), follow=True, callback='my_parse', errback='my_errback'),)           
    custom_settings = {
        'DEPTH_LIMIT': 3,
        #etc
    }
    
    start_urls = ['url']
    allowed_domains = ['domain']
    
    def my_errback(self, failure):
        # log all failures
    
    def parse_start_url(self, response):
        return self.my_parse(response)

    def my_parse(self, response):
        # parse responses

내가 직면 한 문제는 errback이 start_url이 아닌 추출 된 링크에 대해서만 호출된다는 것입니다.

CrawlSpider 및 규칙을 사용하고 있으므로 start_requests 메서드 (아래 참조)를 사용할 수 없습니다. 그렇게하면 start_url 만 스크랩됩니다.

def start_requests(self):
            for u in self.start_urls:
                yield scrapy.Request(u, callback=self.my_parse,
                                        errback=self.my_errback)

답변

J_Z Sep 24 2020 at 07:20

이것이 답입니다. https://stackoverflow.com/a/34345668/13903397

기본적으로 start_requests콜백 매개 변수 를 사용 하되 제거해야합니다. 이렇게하면 기본 self.parse가 호출되고 규칙이 준수됩니다.

class CS(CrawlSpider):
    name = "CS"
    rules = (Rule(LinkExtractor(allow=[], deny=[]), follow=True, callback='my_parse', errback='my_errback'),)           
    custom_settings = {
        'DEPTH_LIMIT': 3,
        #etc
    }
    
    start_urls = ['url']
    allowed_domains = ['domain']

    def start_requests(self):
        for u in self.start_urls:
            yield scrapy.Request(u, errback=self.my_errback)
    
    def my_errback(self, failure):
        # log all failures
    
    def parse_start_url(self, response):
        return self.my_parse(response)

    def my_parse(self, response):
        # parse responses