如何在scrapy中从json中删除转义字符?
我有一个json文件,在某些json字段中有转义字符,因此如何删除转义字符,我的json数据如下所示:如何在scrapy中从json中删除转义字符?,json,python-3.x,scrapy,Json,Python 3.x,Scrapy,我有一个json文件,在某些json字段中有转义字符,因此如何删除转义字符,我的json数据如下所示: {"url": "www.expamle/com", "name": "\n\t\t\t\t\t\tHisense 49\" FHD TV 49B5200PT 49B5200PT", "price": "R5,499.00", "brand":
{"url": "www.expamle/com", "name": "\n\t\t\t\t\t\tHisense 49\" FHD TV 49B5200PT 49B5200PT", "price":
"R5,499.00", "brand": "\n\t\t\t\t\t\tHisense"}
以下是我的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解析(self,response):
对于响应中的tv.xpath(“../div[@class='product-tile-inner']”):
url=tv.xpath(“.//a[@class='product-tile-internal\uu img js tmProductLinkClickEvent']/@href”).get()
name=tv.xpath(“.//a[@class='product-tile-internal\uu img js gtmProductLinkClickEvent']/@title”).get()
price=tv.xpath(“.//p[@class='col-xs-12 price-ONPROMOTION']/text()”).get()
img=tv.xpath(“.//a[@class='product-tile-internal\uu img js gtmProductLinkClickEvent']/@src”).get()
屈服{
“url”:url.strip()如果url为其他url,
“name”:name.strip()如果name-else-name,
'price':price.strip()如果price-else-price,
'img':img.strip()如果img else img
}
您需要strip()
包含空格的字段:
def解析(self,response):
对于响应中的tv.xpath(“../div[@class='product-tile-inner']”):
url=tv.xpath(“.//a[@class='product-tile-internal\uu img js tmProductLinkClickEvent']/@href”).get()
name=tv.xpath(“.//a[@class='product-tile-internal\uu img js gtmProductLinkClickEvent']/@title”).get()
price=tv.xpath(“.//p[@class='col-xs-12 price-ONPROMOTION']/text()”).get()
img=tv.xpath(“.//a[@class='product-tile-internal\uu img js gtmProductLinkClickEvent']/@src”).get()
屈服{
“url”:url.strip()如果url为其他url,
“name”:name.strip()如果name-else-name,
'price':price.strip()如果price-else-price,
'img':img.strip()如果img else img
}
只需使用json.loads()
处理JSONDon您不想使用正则表达式re
?只需使用json.loads()
处理JSONDon您不想使用正则表达式re
?