如何按类别从维基百科中获取数据?

2024-05-10 01:23:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想使用维基百科的医学数据进行分析。我用python进行抓取。 我使用此库在查询中按单词搜索:

import wikipedia

import requests
import pprint
from bs4 import BeautifulSoup
wikipedia.set_lang("en")
query = raw_input()
WikiPage = wikipedia.page(title = query,auto_suggest = True)
cat = WikiPage.categories
for i in cat:
    print i

得到分类。在

但是,我的问题是反过来:

我想给一个类别,例如:健康或医学术语,并得到所有与此类型的文章。在

我该怎么做?在


Tags: 数据fromimportlangwikipediaquery单词requests
1条回答
网友
1楼 · 发布于 2024-05-10 01:23:49

编辑:实际答案

还有API:Categorymembers,它记录了用法、参数并给出了“如何检索给定类别中按标题排序的页面列表”的示例。它不会使您免于不得不自己从类别树(参见下文)中下降,但是您可以获得一个不错的入口点和机器可读的结果。在

旧答案:相关信息

Help:Category页面的Searching for articles in categories部分给出了一个非常简短的指针:

In addition to browsing through hierarchies of categories, it is possible to use the search tool to find specific articles in specific categories. To search for articles in a specific category, type incategory:"CategoryName" in the search box.

An "OR" can be added to join the contents of one category with the contents of another. For example, enter

    incategory:"Suspension bridges" OR incategory:"Bridges in New York City"

to return all pages that belong to either (or both) of the categories, as here.

Note that using search to find categories will not find articles which have been categorized using templates. This feature also doesn't return pages in subcategories.

为了解决子类别问题,可以改用Special:CategoryTree页。但是,页面没有指向明显的文档。所以我认为必须在页面源代码中手动搜索<form>字段,才能创建一个编程API。在

相关问题 更多 >