简体   繁体   中英

How cam I remove escape characters from json in scrapy?

I have a json file that has escape characters in some json field, so how can I remove the escape characters, here is how my json data looks like:

{"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"}

here is my python parse method:

    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()


       }

You need to strip() fields which contain spaces:

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
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM