1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-22 14:32:28 +00:00
sitc/lod/extract_data.py

50 lines
1.4 KiB
Python
Raw Normal View History

2017-02-21 17:02:21 +00:00
import sys
2017-02-27 11:52:40 +00:00
from future.standard_library import install_aliases
install_aliases()
2017-02-21 17:02:21 +00:00
from urllib import request, parse
from rdflib import Graph, term
from lxml import etree
if len(sys.argv) < 2:
print('Usage: python {} <URL>'.format(sys.argv[0]))
print('')
print('Extract rdfa, microdata and json-ld annotations from a website')
exit(1)
url = sys.argv[1]
g = Graph()
g.parse(url, format='rdfa')
g.parse(url, format='microdata')
def sanitize_triple(t):
"""Function to remove bad URIs from the graph that would otherwise
make the serialization fail."""
def sanitize_triple_item(item):
if isinstance(item, term.URIRef) and '/' not in item:
return term.URIRef(parse.quote(str(item)))
return item
return (sanitize_triple_item(t[0]),
sanitize_triple_item(t[1]),
sanitize_triple_item(t[2]))
with request.urlopen(url) as response:
# Get all json-ld objects embedded in the html file
2017-02-21 17:22:14 +00:00
html = response.read().decode('utf-8', errors='ignore')
2017-02-21 17:02:21 +00:00
parser = etree.XMLParser(recover=True)
root = etree.fromstring(html, parser=parser)
2017-02-21 17:22:14 +00:00
if root:
for jsonld in root.findall(".//script[@type='application/ld+json']"):
g.parse(data=jsonld.text, publicID=url, format='json-ld')
2017-02-21 17:02:21 +00:00
fixedgraph = Graph()
fixedgraph += [sanitize_triple(s) for s in g]
2017-02-21 17:22:14 +00:00
print(g.serialize(format='turtle').decode('utf-8', errors='ignore'))