如何解决我如何从scrapy中删除json中的转义字符?
我有一个在某些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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。