pythonpython-2.7python-3.xrdflibfoaf

How do I add image url with graph add in rdflib


I am using rdflib==4.1.2. This is my code

>>> g=rdflib.Graph()
>>> s=rdflib.BNode()
>>> FOAF = rdflib.Namespace("http://xmlns.com/foaf/0.1/")
>>> g.bind('foaf', FOAF)
>>> g.add((s, FOAF['img'],"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcToZGEWvIBFgCiona6d74FtVthl4lkdJg3d61SGy-UCf4qFuDLD"))

I am getting this traceback.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/rdflib/graph.py", line 394, in add
    "Object %s must be an rdflib term" % (o,)
AssertionError: Object https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcToZGEWvIBFgCiona6d74FtVthl4lkdJg3d61SGy-UCf4qFuDLD must be an rdflib term

How do I fix this?

Thanks!


Solution

  • The function Graph.add expects a tuple with three nodes as can be seen in the graph.py code:

    assert isinstance(o, Node)
    

    you can use URIRef to convert your last parameter into one (see also the examples of the documentation):

    g.add((s, FOAF['img'],rdflib.URIRef("https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcToZGEWvIBFgCiona6d74FtVthl4lkdJg3d61SGy-UCf4qFuDLD")))