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

Ignore invalid jsonld elements on the page source. #189

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions extruct/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import jstyleson
import lxml.etree
import logging

from extruct.utils import parse_html

Expand All @@ -28,14 +29,25 @@ def extract_items(self, document, base_url=None):
if items for item in items if item
]

def _may_be_get_json(self, script):
try:
return json.loads(script, strict=False)
except Exception:
return None

def _extract_items(self, node):
script = node.xpath('string()')
try:
# TODO: `strict=False` can be configurable if needed
data = json.loads(script, strict=False)
except ValueError:
data = self._may_be_get_json(script)
# check if valid json.
if not data:
# sometimes JSON-decoding errors are due to leading HTML or JavaScript comments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not remove this comment, I think it's useful

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the comment back to the code.

data = jstyleson.loads(HTML_OR_JS_COMMENTLINE.sub('', script), strict=False)
script = jstyleson.dispose(HTML_OR_JS_COMMENTLINE.sub('', script))
data = self._may_be_get_json(script)
# After processing check if json is still valid.
if not data:
logging.exception('Invalid jsonld element detected %s', script)
return

if isinstance(data, list):
for item in data:
yield item
Expand Down
16 changes: 16 additions & 0 deletions tests/samples/custom.invalid/JSONLD_valid_and_invalid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<script type="application/ld+json">
{ invalid_jsonld_property: invalid_jsonld_value}
</script>
<!-- only the below tag should be parsed -->
<script type="application/ld+json">
{"foo" : "bar"}
</script>
</head>

<body></body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"foo" : "bar"} ]
6 changes: 6 additions & 0 deletions tests/test_jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def test_songkick(self):
'Elysian Fields Brooklyn Tickets, The Owl Music Parlor, 31 Oct 2015'
)

def test_when_page_has_invalid_jsonld_elements_should_skip(self):
self.assertJsonLdCorrect(
folder='custom.invalid',
page='JSONLD_valid_and_invalid'
)

def test_jsonld_empty_item(self):
self.assertJsonLdCorrect(
folder='songkick',
Expand Down