Python中的Wikipedia模块

Python中的Wikipedia模块

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

介绍

互联网是最重要的信息来源。只需一点击,我们就可以获得所有知识,如果我们有互联网连接的话。因此,了解如何从正确的来源收集正确的信息是必要的。当我们从各种来源检索信息时,这个术语称为 数据抓取 。我们都使用过维基百科。它是充满信息的世界。

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

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

安装

该模块包装了官方的维基百科API。在步骤1中,我们将使用以下pip命令安装Wikipedia模块。在终端中输入以下命令-

$pip install wikipedia

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

import wikipedia

我们现在可以从维基百科中提取数据了。

使用维基百科模块入门

维基百科模块包含各种内置方法,可以帮助我们获取所需信息。

搜索标题和结果

Python中的维基百科模块允许我们使用 search() 方法作为参数来搜索查询。该方法返回包含搜索查询的所有文章的列表。让我们来看下面的示例。

示例

import wikipedia
# Seaching a title
print(wikipedia.search("India"))

输出:

['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 参数传递一个值来限制搜索标题的数量。请考虑以下示例。

示例

import wikipedia
# Seaching a title
print(wikipedia.search("India", results = 4))

输出:

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

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

Suggestion

顾名思义,suggest方法返回查询的建议维基百科标题,如果没有找到则返回none。让我们看下面的示例。

示例

import wikipedia

print(wikipedia.suggest("Coronavrdsf"))

输出:

None

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

文章摘要

Python Wikipedia模块提供了summary()方法,它返回文章的摘要或主题。这个方法接受两个参数 – 标题和句子,并以字符串格式返回摘要。让我们看下面的示例。

示例

import wikipedia

print(wikipedia.summary("Rohit Sharma", sentences=4))

输出:

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).

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

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

示例:

print(wikipedia.summary("key"))

输出:

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

提取标题元数据

我们可以获取维基百科页面的完整元数据或文本内容,不包括图像、表格等。这个模块提供了页面对象的内容属性。让我们看下面的示例。

示例

import wikipedia

print(wikipedia.page("Sachin Tendulkar").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.
............

获取完整的维基百科页面数据

Python维基百科模块允许我们使用 page() 函数获取完整的维基百科。它返回页面内容、分类、坐标、图片、链接和其他元数据。让我们来了解下面的示例。

示例

import wikipedia

# wikipedia page object is created
object = wikipedia.page("America")

# printing html of page_object
print(object.html)

# printing title
print(object.original_title)

# printing links on that page object
print(object.links[0:20])

输出:

>
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() 方法来更改页面语言。每种语言都有一个标准的前缀代码,作为参数传递给该方法。让我们了解以下示例。

示例

import wikipedia
wikipedia.set_lang("hi")

print(wikipedia.summary("Python"))

输出:

????? ?? ??????? ??????? ?? ??? ???????, ???? ?????? ???????????? ???? (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的所有重要概念。我们还讨论了如何获取各种信息,例如页面标题、摘要、类别,并从网络中提取数据。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程