Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract JSON-LD with control characters. #84

Closed
shiquanwang opened this issue Jul 31, 2018 · 0 comments
Closed

Extract JSON-LD with control characters. #84

shiquanwang opened this issue Jul 31, 2018 · 0 comments

Comments

@shiquanwang
Copy link
Contributor

shiquanwang commented Jul 31, 2018

Some pages have JSON-LD with control characters.
One example is: https://www.johnlewis.com/sony-xperia-x-smartphone-android-5-4g-lte-sim-free-32gb/p3210080

when you try to extract JSON-LD data from this page, you'll get:
Invalid control character at: line 8 column 353 (char 625)

Maybe need to change JsonLdExtractor._extract_items() in extruct/extruct/jsonld.py as below:

from json import JSONDecodeError

    def _extract_items(self, node):
        script = node.xpath('string()')
        try:
            data = json.loads(script)
        except ValueError:
            # sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
            try:
                data = json.loads(HTML_OR_JS_COMMENTLINE.sub('', script))
            except JSONDecodeError:
                data = json.loads(script, strict=False)
        if isinstance(data, list):
            return data
        elif isinstance(data, dict):
            return [data]
@lopuhin lopuhin closed this as completed in ddb25ae Aug 8, 2018
lopuhin added a commit that referenced this issue Aug 8, 2018
[MRG+2] Fix: #84 try to parse JSON-LD with control characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant