cankaya07
cankaya07

Reputation: 63

Scrapy javascript json object loads

I am scraping one web site which has javascript json object. How can i convert that javascript json object to pure json object. I need JSON.stringfy method like javascript. How can i do that on python.

{
            title: 'Erkek Gri Sandalet',
            description: '<ul><li>Asıl Dış Materyal: Suni Deri</li><li>İç Materyal: Suni Deri</li><li>Taban Materyali: Kauçuk Taban</li></ul>',
            url: '/erkek-gri-sandalet-3500250',
            code: 'C-362686',
            id: '3500250'
}

i am getting an error when passing above string into the json.loads() The error is

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

How can i convert to this javascript json object to native json. I used https://www.freeformatter.com/json-formatter.html website to validate above json and that website validate and converted native JSON easily.

screenshot of online validator

Upvotes: 0

Views: 86

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You could do something funky with regular expressions, but actually you should take advantage of the fact that even though this is not valid JSON, it is valid YAML. Install PyYAML and you can just do yaml.load(data).

Upvotes: 2

Related Questions