使用python通过SPARQL端点将RDF数据存储到Triplestore [英] Store RDF data into Triplestore via SPARQL endpoint using python
问题描述
我正在尝试将以下网址中的数据作为三元组保存到三元组存储中,以供将来查询.这是我的代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
import re
url='http://gnafld.net/address/?per_page=10&page=7'
page = requests.get(url)
response = requests.get(url)
response.raise_for_status()
results = re.findall('\"Address ID: (GAACT[0-9]+)\"', response.text)
address1=results[0]
a = "http://gnafld.net/address/"
new_url = a + address1
r = requests.get(new_url).content
print(r)
运行上面的代码后,我得到了如下答案: 在此处输入图片描述
我的问题是如何将RDF数据插入Fuseki Server SPARQL端点?我尝试这样的代码:
import rdflib
from rdflib.plugins.stores import sparqlstore
#the following sparql endpoint is provided by the GNAF website
endpoint = 'http://gnafld.net/sparql'
store = sparqlstore.SPARQLUpdateStore(endpoint)
gs=rdflib.ConjunctiveGraph(store)
gs.open((endpoint,endpoint))
for stmt in r:
gs.add(stmt)
但似乎不起作用.我该如何解决这个问题?感谢您的帮助!
您在图像中显示的答案是RDF三重格式,只是打印效果不佳.
要将RDF数据存储在RDF存储中,可以使用 RDFlib .这是示例. >
如果您使用Jena Fuseki服务器,则应该能够从python访问它,就像从python访问任何其他SPARQL端点一样.
您可能希望看到我对相关 After I run the code above, I got the answer like:
enter image description here My question is how to insert the RDF data to a Fuseki Server SPARQL endpoint? I try the code like this: But it seems that it does not work. How can I fix this problem? Thanks for your help! The answer you show in the image is in RDF triple format, it is just not pretty printed. To store the RDF data in an RDF store you can use RDFlib. Here is an example of how to do that. If you use Jena Fuseki server you should be able to access it from python just as you access any other SPARQL endpoint from python. You may want to see my answer to a related SO question as well. 这篇关于使用python通过SPARQL端点将RDF数据存储到Triplestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!import rdflib
from rdflib.plugins.stores import sparqlstore
#the following sparql endpoint is provided by the GNAF website
endpoint = 'http://gnafld.net/sparql'
store = sparqlstore.SPARQLUpdateStore(endpoint)
gs=rdflib.ConjunctiveGraph(store)
gs.open((endpoint,endpoint))
for stmt in r:
gs.add(stmt)