Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from xml.etree import ElementTree as ET
import urllib
dbpedia_endpoint = "http://dbpedia.org/sparql?"
ns = {'ns': '{http://www.w3.org/2005/sparql-results#}'}
result_xpath ='%(ns)sresults/%(ns)sresult/%(ns)sbinding/%(ns)sliteral' % ns
def wikipedia_abstract_from_dbpedia(dbpedia_key):
sparql = """
SELECT ?abstract
WHERE {
{ <http://dbpedia.org/resource/%s> <http://dbpedia.org/property/abstract> ?abstract .
FILTER langMatches( lang(?abstract), 'en') }
}
""" % dbpedia_key
url = dbpedia_endpoint + urllib.urlencode({'query': sparql})
et = ET.parse(urllib.urlopen(url))
return et.find(result_xpath).text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment