首页 > 解决方案 > Extract() 位置列表

问题描述

嗨,我尝试从scrapy 项目中提取信息。

我试试

dre = [i.split(', ') for i in response.xpath('normalize-space(//*[contains(@class,"business-address")])').extract()]
    ml_item['address'] = dre[0]

输出:

'address': ['Calle V Centenario 24', '46900', 'Torrente', 'Valencia']

我需要将此输出中的信息保存在以逗号分隔的不同变量中 ml_item['cp'] = '46900'ml_item['city'] = 'Torrente'

标签: pythonxpathscrapy

解决方案


如果dre[0]给你['Calle V Centenario 24', '46900', 'Torrente', 'Valencia']那么

ml_item['cp']   = dre[0][1] 
ml_item['city'] = dre[0][2]

或者

ml_item['address'] = dre[0]

ml_item['cp']   = ml_item['address'][1] 
ml_item['city'] = ml_item['address'][2]

推荐阅读