【发布时间】:2021-10-13 01:04:30
【问题描述】:
我正在尝试使我抓取的文本数据看起来更干净,并删除 <br> 标记或将其替换为 csv 中的实际换行符:
<div>
"This is an example."
<br>
"This is an example too."
<div>
当我使用 xpath 抓取文本并使用 strip() 函数 response.xpath('//div//text()').extract().strip()(我使用 itemloader,所以真正的函数看起来有点不同,但基本相同)时,输出如下所示:
['This is an example text.',
'',
'This is an example too.'],
#data in csv file:
"This is an example text.,This is an example too."
现在我要么想删除 <br> 标记,要么删除整个逗号,所以结果如下所示:"This is an example text. This is an example too"
或者我想用实际的换行符替换它:
"This is an example text.
This is an example too."
我已经尝试了几个 .strip() 命令,即 .strip(u'\u0027') 删除引号或 .strip(u'\00A0') 删除空格,但没有任何效果
我真的可以用scrapy做到这一点吗?如果是的话,有什么想法吗?如果没有,我以后是否必须对 pandas 执行此操作?
【问题讨论】:
-
我看到你已经得到了答案,不过这也可以用 response.xpath('string(//div)').get() 来完成,试试看。
标签: python html web-scraping scrapy