[英]How cam I remove escape characters from json in 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": "\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 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.