не могу отправить запрос в разборе callback в scrapy - PullRequest
0 голосов
/ 19 сентября 2019

У меня есть класс, чтобы отбросить некоторые данные:

class SiteSpider(scrapy.Spider):
    name = "somesite"
    start_urls = ['https://www.somesite.com']

    def start_requests(self):
        parser = CommentParser()
        urls = ['https://www.somesite.com']
        for url in urls:
            yield scrapy.Request(url=url, callback=parser.scrap)

, который в классе CommentParser у меня есть:

class CommentParser():
    def scrap(self, response):
        print("from CommentParser.scrap =>", response.url)
        for i in range(5):
            yield scrapy.Request(url="https://www.somesite.com/comments/?page=%d" % i, callback=self.parse)

    def parse(self,response):
        print("from CommentParser.parse => ", response.url)
        yield dict(response_url = response.url)

, но скрап-доза не отправляет запрос в классе CommentParser, и поэтому я могуне получить ответ в CommentParser.parse

1 Ответ

0 голосов
/ 20 сентября 2019

Вы должны играть с ООП, обратите внимание SiteSpider(CommentParser):, что означает, что SiteSpider будет иметь доступ к методам CommentParser

class CommentParser(scrapy.Spider):
    def scrap(self, response):
        print("from CommentParser.scrap =>", response.url)
        for i in range(5):
            yield scrapy.Request(url="https://www.somesite.com/comments/?page=%d" % i, callback=self.parse)

    def parse(self,response):
        print("from CommentParser.parse => ", response.url)
        yield dict(response_url = response.url)

class SiteSpider(CommentParser):
    name = "somesite"
    start_urls = ['https://www.somesite.com']

    def start_requests(self):
        urls = ['https://www.somesite.com']
        for url in urls:
            yield scrapy.Request(url=url, callback=self.scrap) #This will call CommentParser's scrape method
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...