【问题标题】:scrapy - CrawlSpider is not executing my callbackscrapy - CrawlSpider 没有执行我的回调
【发布时间】:2021-11-18 15:31:44
【问题描述】:

我创建了一个蜘蛛来收集 scratch.mit.edu 上的用户名。 它成功导航到配置文件页面,但它不运行回调函数。我认为这可能与我编写允许属性的方式有关。

我的代码:

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class ForumnSpider(CrawlSpider):
    name = 'forumn'
    allowed_domains = ['scratch.mit.edu']
    start_urls = [
        'https://scratch.mit.edu/users/accountcraft123/'
    ]

    rules = (
        Rule(
            LinkExtractor(),
        ),
        Rule(
            LinkExtractor(
                allow=('/users/'),
            ),
            callback='parse_item',
        ),
    )

    def parse_item(self, response):
        self.logger.info('This is a profile page. %s', response.url)
        response.xpath('//div[@class="header-text"]/h2/text()').get()

【问题讨论】:

    标签: python scrapy web-crawler


    【解决方案1】:

    @iL0g1c,到目前为止它正在工作

    import scrapy
    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    
    class ForumnSpider(CrawlSpider):
        name = 'forumn'
        allowed_domains = ['scratch.mit.edu']
        start_urls = ['https://scratch.mit.edu/users/accountcraft123/']
    
        rules = (
            Rule(LinkExtractor(allow=(r'/users/.*')), follow=True,callback='parse'),
    
        )
    
    
        def parse(self, response):
            self.logger.info('This is a profile page. %s', response.url)
            response.xpath('//div[@class="header-text"]/h2/text()').get()
    

    【讨论】:

    • 谢谢。回调似乎现在正在工作。我唯一改变的是回调名称。由于 parse 已经在 scrapy 中使用,我将其重命名为 parse_item 以防止覆盖。
    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多