gpt4 book ai didi

python - 如何删除 scrapy 输出中的逗号

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:00 26 4
gpt4 key购买 nike

我正在研究一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com