【发布时间】:2021-04-23 18:29:53
【问题描述】:
我正在使用Python wikipedia library 获取页面类别列表。我看到它是MediaWiki API 的包装器。
无论如何,我想知道如何将类别概括为 marco 类别,例如这些 Main topic classifications。
例如,如果我搜索页面Hamburger,有一个名为German-American cousine 的类别,但我想获得它的超级类别,例如Food and Drink。我该怎么做?
import wikipedia
page = wikipedia.page("Hamburger")
print(page.categories)
# how to filter only imortant categories?
>>>['All articles with specifically marked weasel-worded phrases', 'All articles with unsourced statements', 'American sandwiches', 'Articles with hAudio microformats', 'Articles with short description', 'Articles with specifically marked weasel-worded phrases from May 2015', 'Articles with unsourced statements from May 2017', 'CS1: Julian–Gregorian uncertainty', 'Commons category link is on Wikidata', 'Culture in Hamburg', 'Fast food', 'German-American cuisine', 'German cuisine', 'German sandwiches', 'Hamburgers (food)', 'Hot sandwiches', 'National dishes', 'Short description is different from Wikidata', 'Spoken articles', 'Use mdy dates from October 2020', 'Webarchive template wayback links', 'Wikipedia articles with BNF identifiers', 'Wikipedia articles with GND identifiers', 'Wikipedia articles with LCCN identifiers', 'Wikipedia articles with NARA identifiers', 'Wikipedia indefinitely move-protected pages', 'Wikipedia pages semi-protected against vandalism']
我没有找到一个 api 来遍历 Wikipedia Categories 的层次结构树。
我接受 Python 和 API 请求解决方案。谢谢
编辑: 我找到了 api categorytree,它似乎做了一些类似于我需要的事情。
无论如何,我找不到插入 options 参数的方法,如文档中所述。我认为选项可以是在这个link中表达的那些,比如mode=parents,但是我找不到在HTTP url中插入这个参数的方法,因为它必须是一个JSON对象,如文档中所说.我正在尝试这个https://en.wikipedia.org/w/api.php?action=categorytree&category=Category:Biscuits&format=json。如何插入options字段?
【问题讨论】:
-
categorytree是一个陈旧而丑陋的 API,用于在 UI 中呈现类别树的特定目的。使用categories或categorylinks dump 可能会更好。 -
你能定义“重要”吗?
-
@horcrux 我看不出我在哪里写了“重要”。如果您指的是搜索更一般的类别,我的目的应该是找到最高父类别以概括每个维基百科页面的类别。我想要的分类示例是en.wikipedia.org/wiki/Category:Main_topic_classifications
-
您说“如何仅过滤重要的类别?” (“imortant”中有错字)。因此,为了更好地定义您的问题:给定类别 X,您想在“类别:主要主题分类”中的类别中获得类别 Y,这样 X 包含在 Y 中。我说的对吗?
-
你是对的@horcrux。这正是我的目的:)
标签: python mediawiki wikipedia wikipedia-api mediawiki-api