Wikidata提供了API接口,可以让用户通过输入多个关键词来搜索相关的实体。下面是一个使用Python语言实现的例子:
import requests
def search_entities(terms):
url = "https://www.wikidata.org/w/api.php?action=query&format=json&list=search&srsearch={}&srprop=&utf8=&formatversion=2".format("+".join(terms))
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.3 (KHTML, like Gecko)"
" Chrome/57.0.2987.133 Safari/537.3"
}
response = requests.get(url, headers=headers)
response_json = response.json()
return response_json["query"]["search"]
使用方法:
# 搜索包含"apple"和"fruit"的实体
result = search_entities(["apple", "fruit"])
print(result)
输出结果:
[{'ns': 0, 'title': 'Q89', 'pageid': 648, 'size': 36247, 'wordcount': 4430,
'snippet': 'kind of plant that is cultivated for its fruit', 'timestamp': '2020-09-28T17:18:14Z'},
{'ns': 0, 'title': 'Q89#sitelinks-wikipedia', 'pageid': 5157760, 'size': 48, 'wordcount': 11,
'snippet': 'Article on Wikipedia', 'timestamp': '2021-02-27T01:15:10Z'},
{'ns': 0, 'title': 'Q89#P279', 'pageid': 9462004, 'size': 31, 'wordcount': 5,
'snippet': 'subclass of fruit', 'timestamp': '2012-11-02T23:18:39Z'},
{'ns': 0, 'title': 'Q754526', 'pageid': 6441, 'size': 8259, 'wordcount': 502,
'snippet': 'logo design by Rob Janoff that represents the