I am using the following query to get wikidata ID from dbpedia page using owl:sameas
.
SELECT distinct ?wikidata_concept
WHERE {<http://dbpedia.org/resource/Category:Michael_Jackson> owl:sameAs ?wikidata_concept
FILTER(regex(str(?wikidata_concept), "www.wikidata.org" ) )}
LIMIT 100
It works fine on Virtuoso SPARQL Query Editor. I get http://www.wikidata.org/entity/Q7215695
as the answer which is correct.
However, when I try to do the same using SPARQLWrapper
in python, I don't get the above answer (basically the data frame in empty).
My python code is as follows.
import pandas as pd
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://live.dbpedia.org/sparql")
item = "http://dbpedia.org/resource/Category:Michael_Jackson"
sparql.setQuery(f"SELECT distinct ?wikidata_concept WHERE {{<{item}> owl:sameAs ?wikidata_concept FILTER(regex(str(?wikidata_concept), \"www.wikidata.org\" ) )}} LIMIT 100")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print(results)
results_df = pd.io.json.json_normalize(results['results']['bindings'])
print(results_df)
Please let me know where I am making things wrong. I am happy to provide more details if needed.