DBPedia资源rdfs:type查询返回空结果的问题排查
Hey there! Let's break down why your SPARQL query isn't returning the results you expect. The core issue here is a mistake in the resource URI path you're using.
核心问题:资源URI路径错误
You're using http://dbpedia.org/page/Energy as the resource identifier, but DBPedia's RDF dataset uses the /resource/ path for its actual RDF resources. The /page/ path is only for the human-readable Wikipedia-style web pages, not the machine-readable RDF data that SPARQL queries interact with.
修复方案
1. 修正资源URI
Replace the /page/ segment in your URI with /resource/. The correct resource URI for Energy should be:http://dbpedia.org/resource/Energy
2. 调整代码逻辑
You can either update the resource variable directly, or add a quick replacement in your code to handle this automatically:
# Fix the URI path if it uses "/page/" corrected_resource = resource.replace("/page/", "/resource/") query = """SELECT * WHERE { <""" + corrected_resource + """> rdfs:type ?subject. }"""
3. 正确的查询示例
The fixed query will look like this:
SELECT * WHERE { <http://dbpedia.org/resource/Energy> rdfs:type ?subject. }
This query will return all the rdfs:type relationships you mentioned, including owl:Thing, dbo:Building, and all the YAGO category types.
额外小提示
Make sure you're querying DBPedia's official SPARQL endpoint: https://dbpedia.org/sparql — using the wrong endpoint can also lead to empty results. If you ever run into resource redirects in the future, you can add a check for dbo:wikiPageRedirects to find the target resource, but that's not needed here.
内容的提问来源于stack exchange,提问作者Vid Stropnik

