scrapy 使用变量动态创建收益字段

问题描述 投票:0回答:1

我想用 scrapy 从亚马逊产品页面获取所有要点,例如亚马逊链接,但数量各不相同。我最终使用了这样的东西

def parse(self, response):
        t = response
        url = t.request.url
        yield{
                'bullets_no': len(t.xpath('//div[@id="feature-bullets"]//li/span/text()'))
                'bullet_1' : t.xpath('//div[@id="feature-bullets"]//li/span/text()')[0].get().strip()
                'bullet_2' : t.xpath('//div[@id="feature-bullets"]//li/span/text()')[1].get().strip()
                'bullet_3' : t.xpath('//div[@id="feature-bullets"]//li/span/text()')[2].get().strip()
                'bullet_4' : t.xpath('//div[@id="feature-bullets"]//li/span/text()')[3].get().strip()
                'bullet_5' : t.xpath('//div[@id="feature-bullets"]//li/span/text()')[4].get().strip()
...
            }

但是在 pythong 中我可以简单地做这样的事情并自动调整:

bullets = t.xpath('//div[@id="feature-bullets"]//li/span/text()')
    for i, bullet in enumerate(bullets):
        row[f'Bullet_{i+1}'] = bullet.strip()

是否可以在 scrapy 中创建这样的生成字段?

python scrapy
1个回答
0
投票

使用

getall()
在这种情况下你会得到列表

def parse(self, response):
    t = response
    url = t.request.url
    yield{f"bullet_{i}": el.strip() for i, el in enumerate(t.xpath('//div[@id="feature-bullets"]//li/span/text()').getall(), start=1) }
© www.soinside.com 2019 - 2024. All rights reserved.