Python 中的 Wikipedia 模块

17 Mar 2025 | 6 分钟阅读

在本文中,我们将讨论 Python 中的 Wikipedia 模块,并讨论如何利用 Python 脚本使用 Wikipedia 模块。我们将从 Wikipedia 获取各种信息。

引言

互联网是信息最重要的来源。只要有互联网连接,我们就可以随时获取所有知识。因此,了解如何从正确来源收集正确信息至关重要。当我们从各种来源检索信息时,这个过程称为 **数据抓取**。我们都使用过 Wikipedia。它是信息信息的宝库。

Wikipedia 是互联网上最大的平台,包含海量信息。它是一个开源平台,由志愿者编辑社区使用 wiki 编辑系统进行管理。它是一个多语言百科全书。

Python 提供了 **Wikipedia 模块(或 API)** 来抓取维基百科页面的数据。该模块允许我们获取和解析维基百科上的信息。简单来说,我们可以说它是一个小的抓取器,只能抓取有限的数据量。在开始使用它之前,我们需要在本地机器上安装此模块。

安装

此模块封装了官方的 Wikipedia API。在第一步,我们将使用以下 pip 命令安装 Wikipedia 模块。在终端中输入以下命令:

上述命令将在系统中安装该模块。现在,我们需要使用以下命令导入它。

现在我们可以开始从 Wikipedia 中提取数据了。

开始使用 Wikipedia 模块

Wikipedia 模块包含各种内置方法,可帮助获取所需信息。

搜索标题和结果

Python Wikipedia 模块允许我们使用 **search()** 方法搜索作为参数提供的查询。此方法返回包含搜索查询的所有文章的列表。让我们理解以下示例。

示例 -

输出

['India', 'Constitution of India', 'Demographics of India', 'Languages of India', 'Republic Day (India)', 'Government of India', 'Economy of India', 'History of India', 'The Times of India', 'List of prime ministers of India']

正如我们在上面的输出中看到的,该方法返回了标题和相关搜索。我们可以通过为 **result** 参数传递一个值来限制搜索标题的数量。考虑以下示例。

示例 -

输出

['India', 'Constitution of India', 'Demographics of India', 'Languages of India']

上面的代码打印了四个结果,因为我们请求只获取四个结果。

建议

顾名思义,suggest 方法返回查询的建议 Wikipedia 标题,如果没有找到任何标题则返回 None。让我们看下面的例子。

示例 -

输出

None

在上面的代码中,我们搜索了“Coronavirus”,但输入了错误的拼写。**suggest()** 方法返回 None,因为它没有找到搜索的查询。

文章摘要

Python Wikipedia 模块提供了 **summary()** 方法,该方法返回文章的摘要或主题。此方法接受两个参数 - title 和 sentences,并以字符串格式返回摘要。让我们看下面的例子。

示例 -

输出

Rohit Gurunath Sharma (born 30 April 1987) is an Indian international cricketer who plays for Mumbai in domestic cricket and captains Mumbai Indians in the Indian Premier League as a right-handed batsman and an occasional right-arm off break bowler. He is the vice-captain of the Indian national team in limited-overs formats.
Outside cricket, Sharma is an active supporter of animal welfare campaigns. He is the official Rhino Ambassador for WWF-India and is a member of People for the Ethical Treatment of Animals (PETA).

打印了给定标题的摘要,并且我们通过使用 sentences 参数自定义了摘要文本中要显示的句子数量。

请注意,如果页面不存在,**summary()** 方法会引发“消歧错误”。让我们理解以下示例。

示例 -

输出

Traceback (most recent call last):
  File "C:/Users/DEVANSH SHARMA/PycharmProjects/MyPythonProject/pillow_image.py", line 194, in 
    print(wikipedia.summary("key"))
  File "C:\Users\DEVANSH SHARMA\PycharmProjects\MyPythonProject\venv\lib\site-packages\wikipedia\util.py", line 28, in __call__
    ret = self._cache[key] = self.fn(*args, **kwargs)
  File "C:\Users\DEVANSH SHARMA\PycharmProjects\MyPythonProject\venv\lib\site-packages\wikipedia\wikipedia.py", line 231, in summary
    page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
  File "C:\Users\DEVANSH SHARMA\PycharmProjects\MyPythonProject\venv\lib\site-packages\wikipedia\wikipedia.py", line 276, in page
    return WikipediaPage(title, redirect=redirect, preload=preload)
  File "C:\Users\DEVANSH SHARMA\PycharmProjects\MyPythonProject\venv\lib\site-packages\wikipedia\wikipedia.py", line 299, in __init__
    self.__load(redirect=redirect, preload=preload)raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)
wikipedia.exceptions.DisambiguationError: "Key" may refer to: 
Key (cryptography)
Key (lock)
Key (map)
typewriter
test
Cay
Key, Alabama
Key, Ohio
Key, West Virginia
Keys, Oklahoma
Florida Keys

提取标题的元数据

我们可以获取 Wikipedia 页面的完整元数据或文本内容,但不包括图像、表格等。此模块提供了页面对象的 content 属性。让我们看下面的例子。

示例 -

输出

Sachin Ramesh Tendulkar ( (listen); born 24 April 1973) is an Indian former international cricketer who served as captain of the Indian national team. He is widely regarded as one of the greatest batsmen in the history of cricket. He is the highest run scorer of all time in International cricket. Considered as the world's most prolific batsman of all time, he is the only player to have scored one hundred international centuries, the first batsman to score a double century in a One Day International (ODI), the holder of the record for the most runs in both Test and ODI cricket, and the only player to complete more than 30,000 runs in international cricket. In 2013, he was the only Indian cricketer included in an all-time Test World XI named to mark the 150th anniversary of Wisden Cricketers' Almanac.
............

获取完整的 Wikipedia 页面数据

Python Wikipedia 模块允许我们使用 **page()** 函数获取完整的 Wikipedia。它返回页面内容、类别、坐标、图像、链接和其他元数据。让我们理解以下示例。

示例 -

输出

>
United States
['.as', '.com', '.edu', '.gov', '.gu', '.mil', '.mp', '.net', '.org', '.pr', '.um', '.us', '.vi', '100th meridian west', '117th United States Congress', '1790 United States Census', '1800 United States Census', '1810 United States Census', '1820 United States Census', '1830 United States Census']

自定义页面语言

我们可以更改现有页面的默认语言。**set_lang()** 方法用于更改页面语言。每种语言都有一个标准的语言代码前缀,该前缀作为参数传递给方法。让我们理解以下示例。

示例 -

输出

????? ?? ??????? ??????? ?? ??? ???????, ???? ?????? ???????????? ???? (General Purpose and High Level Programming language), ???????????, ???????? ?????????, ???????????? ???? ??? ?? ???? ?? ?? ??? ?? ?????? ???? ??? ?? ???? ????? ???? ?? ??? ????? ?? ???? ?? ???? ?? ?????
???? ???????????? ?????? ?? ??????, ?????? ???-??????? ?? ??????? ?? ??? ????? ?????? ( {} ) ?? ???????? ???? ???? ??, ????? ??? ???-??????? ?? ??????? ?? ??? ?????? ????? (white space) ?? ?????? ???? ???? ??? ?? ???????????? ???? ?? Guido van Rossum ?? 1991 ??? ????? ??? ?? ??????? ?? ???????????? ???? ?? ?????? ????????? ????? ?? ??? ??? ?? ??????, ???? ?????-??????? ???? ?? ????? ???? ??? ????? "????? ???? ?? ??? ???? ?????? ????????? ?????" ?? ???? ???? ??? ?? ???? ???? ????????? (standard library) ???? ?? ?????? ???
?? ???? ?? ??????-????? ??? ???-??????? (code readability) ?? ??? ???? ??? ??? ????? ?? ???? ?? ?? ???? ????????? ???? ?????? ??; ???? ???? ????????? ????? ?? ?????????? (comprehensive) ??? ?? ?????? ???????? ?? ??? ????? ?????? ????? ??? (pre-installed) ??? ???
???? ?????? ?????? ?? ???, ????? ????? ?? ???????????? ???? ?? ??? ??? ?????? ???? ???? ??, ????? ??? ??? ??? ???????????? ???????? ?? ?? ??????? ?????? ??? ?? ?????? ???? ???? ??? ??? ??????? ?? ????? ????, ????? ??? ?????????? ???????? ????? ????????? (???????????? ?????????) ?? ??? ??? ??? ???? ?? ???? ??? ????? ??????????? ?? ???????? ?????? ?? ??? ?????? ????

正如我们在上面的代码中看到的,它将请求的页面转换成了印地语。我们可以使用 **set_lang()** 方法更改任何语言。

结论

我们已经使用 Python 代码涵盖了 Wikipedia API 的所有重要概念。我们还讨论了如何获取各种信息,例如页面标题、摘要、类别以及从 Web 中提取数据。