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

Python scrapy list return:如何处理/提取列表中的每个元素?,python,xpath,scrapy,scrapy-spider,siblings,Python,Xpath,Scrapy,Scrapy Spider,Siblings,我想问一下,如何在一个变量中处理提取的数据列表。由于(xpath)选择器只提取first.extract_first()或所有content.extract(),我想知道如何迭代并只提取一个元素…比如.extract()[I]和I=I+1。。。那该怎么说呢 这似乎很明显,但在这一点上,我不明白如何利用ItemLoader、Pipeline或任何零碎的文档来解决这个问题 item ['author'] = sel.xpath('.//a[contains(@data-hook, "review-a

我想问一下,如何在一个变量中处理提取的数据列表。由于(xpath)选择器只提取first.extract_first()或所有content.extract(),我想知道如何迭代并只提取一个元素…比如.extract()[I]和I=I+1。。。那该怎么说呢

这似乎很明显,但在这一点上,我不明白如何利用ItemLoader、Pipeline或任何零碎的文档来解决这个问题

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???

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

如果您有一个列表,您可以使用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.

您可以使用
循环对列表进行迭代:

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