如何在scratch中从json中删除转义字符



我有一个json文件,它在一些json字段中有转义字符,所以我如何删除转义字符,下面是我的json数据的样子:

{"url": "www.expamle/com", "name": "nttttttHisense 49" FHD TV 49B5200PT 49B5200PT", "price": 
"R5,499.00", "brand": "nttttttHisense"}

这是我的python解析方法:

def parse(self, response):
for tv in response.xpath(".//div[@class='product-tile-inner']"):
yield{
'url' : tv.xpath(".//a[@class='product-tile-inner__img js- 
gtmProductLinkClickEvent']/@href").get(),
'name' : tv.xpath(".//a[@class='product-tile-inner__img js- 
gtmProductLinkClickEvent']/@title").get(),
'price' : tv.xpath(".//p[@class='col-xs-12 price ONPROMOTION']/text()").get(),
'img' : tv.xpath(".//a[@class='product-tile-inner__img js- 
gtmProductLinkClickEvent']//@src").get()

}

您需要strip()包含空格的字段:

def parse(self, response):
for tv in response.xpath(".//div[@class='product-tile-inner']"):
url = tv.xpath(".//a[@class='product-tile-inner__img js-tmProductLinkClickEvent']/@href").get()
name = tv.xpath(".//a[@class='product-tile-inner__img js-gtmProductLinkClickEvent']/@title").get()
price = tv.xpath(".//p[@class='col-xs-12 price ONPROMOTION']/text()").get()
img = tv.xpath(".//a[@class='product-tile-inner__img js-gtmProductLinkClickEvent']//@src").get()
yield {
'url': url.strip() if url else url,
'name': name.strip() if name else name,
'price': price.strip() if price else price,
'img': img.strip() if img else img
}

最新更新