python-3.x - 如何从 json 文件中删除 \n
问题描述
当我尝试使用终端将抓取的数据保存在 json 文件中时,在 Brand 和 Brand name ({"Brand": "\n Libra\n ", "Price": "$24.95"},) 后面有一个 \n。如何解决这个问题呢。
import scrapy
class GlassSpider(scrapy.Spider):
name = 'glass'
allowed_domains = ['www.glassesshop.com']
start_urls = ['https://www.glassesshop.com/bestsellers/']
def parse(self, response):
for item in response.xpath("//div[@class='col-12 pb-5 mb-lg-3 col-lg-4 product-list-row text-center product-list-item']"):
yield {
'Brand': item.xpath(".//div[@class='p-title']/a/text()").get(),
'Price': item.xpath(".//div[@class='product-title p-tab p-tab-13145']/span/text()").get()
}
解决方案
您可以使用str
replace
方法:
...
yield {
'Brand': item.xpath(".//div[@class='p-title']/a/text()").get("").replace("\n",""),
'Price': item.xpath(".//div[@class='product-title p-tab p-tab-13145']/span/text()").get("").replace("\n","")
}
推荐阅读
- python-3.x - 如何解决这个问题
- javascript - 使用 .map 且条件评估为 true 时递增 1
- javascript - 从另一个对象数组创建一个对象数组并向其添加更多属性
- mysql - SQL Query 不断返回多行
- c# - 进行 SSL 连接时需要主机名验证
- c# - 如何获取不匹配的字符?
- javascript - JavaScript 是否支持复合正则表达式?
- javascript - 使用带有 html5 自定义数据属性的 sortable.js
- javascript - 涉及“.replace”函数的语法问题
- html - 如何在另一个 div 元素上方获取 Ajax 搜索结果?