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

Solves issue #171 #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions extruct/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def extract_items(self, document, base_url=None):

def _extract_items(self, node):
script = node.xpath('string()')
script = self.clean_special_characters(script)
try:
# TODO: `strict=False` can be configurable if needed
data = json.loads(script, strict=False)
Expand All @@ -41,3 +42,38 @@ def _extract_items(self, node):
yield item
elif isinstance(data, dict):
yield data

def clean_special_characters(self, script):
special_character_dict = {
'\\x27': "\'",
'\\x22': '\"',
'\\x3c': '<',
'\\x3e': '>',
'\\x2f': '/',
'\\x5c': '\\',
'\\x2a': '*',
'\\x3d': '=',
'\\x3f': '?',
'\\x21': '!',
'\\x7b': '{',
'\\x7d': '}',
'\\x7c': '|',
'\\x5e': '^',
'\\x60': '`',
'\\x40': '@',
'\\x23': '#',
'\\x24': '$',
'\\x25': '%',
'\\x5b': '[',
'\\x5d': ']',
'\\x5f': '_',
'\\x3a': ':',
'\\x3b': ';',
'\\x2c': ',',
'\\x2e': '.',
'\\x2d': '-',
'\\x2b': '+',
}
for key, value in special_character_dict.items():
script = script.replace(key, value)
return script