我正在研究一个 scrapy 蜘蛛。这是我的代码:
item = MyItem()
item['Description'] = response.xpath('//*[@id="textepresentation"]//p').extract()
item['Description'] = [ '\,'.join(field.split(',')) for field in item.get('Description', [])]
我得到这个结果:
[ u'<p>Some text.</p>',
u'<p>\xa0</p>',
u'<p>\xa0</p>',
u'<p>\xa0</p>',
u'<p>Some other text.</p>',
u'<p>\xa0</p>',
u'<p>\xa0</p>'],
我想删除空 <p> </p>
标签:
item['Description'] = [ ''.join(field.replace('<p>'u'\xa0''</p>', '').encode('utf-8')) for field in item.get('Description', [])]
我得到这个结果:
['<p>Some text.</p>',
'',
'',
'',
'<p>Some other text.</p>',
'',
''],
现在,我怎样才能删除结果中的逗号来得到这个?
['<p>Some text.</p>'
'<p>Some other text</p>']
最佳答案
您需要获取text()
并调用extract_first()
:
response.xpath('//*[@id="textepresentation"]//p/text()').extract_first()
关于python - 如何删除scrapy输出中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294849/