繁体   English   中英

Scrapy仅产生最后一个元素

[英]Scrapy yields only the last element

我在scrapy的帮助下刮了一些课程/课程,但是它似乎只产生列表的最后一个元素。
这是有问题的代码:

def parse_course_list(self, response):
    """ Scrape list of lessons for each course """
    lessons = response.css('ul.lessons-list a')
    for lesson in lessons:
        title = lesson.xpath("text()").extract_first().strip()
        link = lesson.xpath("@href").extract_first().strip()

        url = response.urljoin(link)
        item = response.meta['item']
        item['Lesson'] = title
        item['URL'] = link

        yield scrapy.Request(url, \
            callback=self.parse_lesson,
            meta={'item': item} \
        )

因此,基本上,我是在总结课程并提出详细信息页面的请求。 但是,该课程在parse_lesson函数中始终是相同的。
我在这里完全错过了什么吗?

啊...经典指针问题!

我不知道为什么会发生这种情况,除了您产生的请求携带栈中具有相同地址的项目外。

解决方法如下:

def parse_course_list(self, response):
    lessons = response.css('ul.lessons-list a')
    itemToCopy = response.meta['item']
    for lesson in lessons:
        item=itemToCopy.copy()
        ...

剩下的就是减去item = response.meta['item']

告诉我情况如何。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM