gpt4 book ai didi

python - Scrapy 爬虫,去除字符串中的逗号

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

def parse_item(self, response):
for jobs in response.xpath('//div[@itemtype="http://schema.org/JobPosting"]'):
item = IndeedCoUkItem()
item["jobtitle"] = jobs.xpath('*[@class="jobtitle"]/a//text()').extract()
yield item

项目保存为 CSV 文件,

职位

"高级,嵌入式,,软件,工程师"

你好,

以上是我的 scrapy 爬虫代码的一个片段。我想让输出没有逗号和空格。也就是从“Senior Embedded Software Engineer”到这个“Senior Embedded Software Engineer”。我尝试使用像 ..extract()[0].replace(",","") 这样的 replace(),但它没有用。有什么帮助/建议吗?

最佳答案

您是否尝试打印/记录进入 item['jobtitle] 字段的列表?如果它是一个列表(好吧,它是一个列表),那么导出到 CSV 文件会将此列表转换为逗号分隔的条目。

尝试查看结果并将列表加入一个:

item["jobtitle"] = ' '.join(jobs.xpath('*[@class="jobtitle"]/a//text()').extract())

如果项目包含额外的空白但不是全部,您可以在元素上使用 mapstrip:

item["jobtitle"] = ' '.join(map(unicode.strip,jobs.xpath('*[@class="jobtitle"]/a//text()').extract()))

这会遍历所有元素并去除开头和结尾的空白。

或者,您可以使用 XPath 的 normalize-space:

item["jobtitle"] = ' '.join(jobs.xpath('normalize-space(*[@class="jobtitle"]/a//text())').extract())

关于python - Scrapy 爬虫,去除字符串中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563463/

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