Skip to content

Instantly share code, notes, and snippets.

@simonw
Created January 16, 2010 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonw/279097 to your computer and use it in GitHub Desktop.
Save simonw/279097 to your computer and use it in GitHub Desktop.
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