scrapy list return:如何处理/提取列表的每个元素?

问题描述 投票:2回答:2

我想问一下如何处理一个变量中提取数据的列表。由于(xpath)选择器只提取第一个.extract_first()或所有内容.extract(),我想知道,我如何迭代并只提取一个元素......如.extract()[i]和i = i + 1 ......那怎么样?

它似乎很明显,但在这一点上,我不明白如何利用项目加载器,管道或任何scrapy文档提供来解决这个问题。

item ['author'] = sel.xpath('.//a[contains(@data-hook, "review-author")]/text()').extract_first()

item ['author'] = sel.xpath('.//a[contains(@data-hook, "review-author")]/text()').extract()[0]

item ['author'] = sel.xpath('.//a[contains(@data-hook, "review-author")]/text()').extract()[i] ... i=i+1???

如果你能指出我正确的方向,我将非常感激!

python xpath scrapy scrapy-spider siblings
2个回答
0
投票

您可以使用for循环遍历列表:

for author in sel.xpath('.//a[contains(@data-hook, "review-author")]/text()').extract():
    item ['author'] = author

0
投票

如果你有一个列表,你可以使用for循环迭代它。

item ['author'] = sel.xpath('.//a[contains(@data-hook, "review-author")]/text()').extract()

// Using this for-loop construct instead of indices avoids off-by-one errors
// and the code won't run if the list is empty.
for element in item['author']:
    print element
    // Do whatever you want with the element.
© www.soinside.com 2019 - 2024. All rights reserved.