3

How can I get information about an entity from DBpedia using Python?

Eg: I need to get all DBpedia information about USA. So I need to write the query from python (SPARQL) and need to get all attributes on USA as result.

I tried :

PREFIX db: <http://dbpedia.org/resource/>
SELECT ?p ?o
WHERE { db:United_States ?p ?o }

But here all DBpedia information is not displaying.

How can I do this and which all are the possible plugins/api available for python to connect with DBpedia ?

Also what will be the SPARQL query for generating the above problem result?

KT12
  • 240
  • 2
  • 14
Sreejithc321
  • 1,920
  • 3
  • 18
  • 33
  • 3
    Asking for all the plugins/API is waaay too broad for this. StackExchange is not a replacement search engine for the lazy. I could show you one way to do it, but you are asking for every possible way. Refine your question or it'll probably get closed. – Spacedman Jan 14 '15 at 11:50
  • Hi Spacedman, Which is the best api and bet way to get the data from DBpedia – Sreejithc321 Jan 14 '15 at 12:33
  • The best API for DBpedia is the RDF download, so you can process it locally without hitting on an API at all. – Has QUIT--Anony-Mousse Jan 15 '15 at 08:01

1 Answers1

5

You do not need a wrapper for DBPedia, you need a library that can issue a SPARQL query to its SPARQL endpoint. Here is an option for the library and here is the URL to point it to: http://dbpedia.org/sparql

You need to issue a DESCRIBE query on the United_States resource page:

PREFIX dbres: <http://dbpedia.org/resource/>

DESCRIBE dbres:United_States

Please note this is a huge download of resulting triplets.

Here is how you would issue the query:

from SPARQLWrapper import SPARQLWrapper, JSON

def get_country_description():
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    sparql.setReturnFormat(JSON)

    sparql.setQuery(query)  # the previous query as a literal string

    return sparql.query().convert()
logc
  • 731
  • 3
  • 12
  • Thank you logc. Yes here you are limiting attribute as 'EuropeanCountries' from all countries but what I would like to have is given a country name and then list (in json or just print) all information about that country from dbpedia. – Sreejithc321 Jan 14 '15 at 13:33
  • I am aware of that, that is why I said 'a bit of work is needed'. If I have time, I will edit the answer; otherwise, consider it a starting point :) – logc Jan 14 '15 at 14:01
  • Edited to fit the question more precisely – logc Jan 14 '15 at 16:39