Search code examples
pythonhtml-parsingsemantic-markuprdflibrdfa

Parsing HTML+RDFa in RDFLib


RDFLib seems to support parsing RDFa data. Upon implementing a snippet to parse an RDFa-annotated HTML page, I run into this problem:

Traceback (most recent call last):
  File "/home/zonk/.local/lib/python3.8/site-packages/rdflib/plugin.py", line 107, in get
    p = _plugins[(name, kind)]
KeyError: ('html', <class 'rdflib.parser.Parser'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "basic-rdfa.py", line 7, in <module>
    g.parse("beatles.rdfa.html", format='html') 
  File "/home/zonk/.local/lib/python3.8/site-packages/rdflib/graph.py", line 1209, in parse
    parser = plugin.get(format, Parser)()
  File "/home/zonk/.local/lib/python3.8/site-packages/rdflib/plugin.py", line 109, in get
    raise PluginException("No plugin registered for (%s, %s)" % (name, kind))
rdflib.plugin.PluginException: No plugin registered for (html, <class 'rdflib.parser.Parser'>)

The following snippet is used:

from rdflib import Graph, plugin
g = Graph()
g.parse("beatles.rdfa.html", format='html') 

for subj, pred, obj in g:
    if(subj, pred, obj) not in g:
        raise Exception("Zonk!")

print(f"Graph g has {len(g)} statements.")
print(g.serialize(format="turtle"))

with the following dummy data:

<!DOCTYPE html>

<html lang="en">
<head>
  <title>John Lennon</title>
</head>

<div vocab="http://schema.org/">
  <div typeof="Person">
    <link property="rdfa:copy" href="#lennon"/>
    <link property="rdfa:copy" href="#band"/>
  </div>
  <p resource="#lennon" typeof="rdfa:Pattern"> 
    Name: <span property="name">John Lennon</span>
  <p>
  <div resource="#band" typeof="rdfa:Pattern">
    <div property="band" typeof="MusicGroup">
      <link property="rdfa:copy" href="#beatles"/>
    </div>
  </div>
  <div resource="#beatles" typeof="rdfa:Pattern">
    <p>Band: <span property="name">The Beatles</span></p>
    <p>Size: <span property="size">4</span> players</p>
  </div>
</div>
</html>

Indeed there is no line in plugin.py that registers any HTML data. How can I parse the rdfa-annotated data in this case?

Thank you in advance.


Solution

  • Sorry but the documentation is out of date, we don't support RDFa parsing any more in the current RDFlib version (6.0.0) or the previous version (5.0.0)

    To get RDFa support, you will have to use RDFlib 4.2.2 (https://github.com/RDFLib/rdflib/tree/4.2.2).