如何在 Python 中提取维基百科数据


在本文中,我们将了解如何使用 Python 提取维基百科数据。Python 被广泛用于创建网络爬虫,从网站上捕获元信息。

对于本文,我们将使用维基百科 API 和库从维基百科源 URL 获取数据。API 将帮助从给定的 URL 中获取数据。然后,我们将调用给定 URL 上的方法并在屏幕上打印信息。

为了从维基百科中提取数据,我们必须首先使用“pip install wikipedia”在 Python 中导入维基百科库

在这个程序中,我们将在维基百科中提取 Python 编程的摘要,并将其打印在文本框内。

实例

#Import the tkinter library
from tkinter import *
import tkinter as tk
import wikipedia

win = Tk()
win.geometry("700x500")
win.title("Wikipedia Summary")

result = wikipedia.search("Python Programming")

# get the page Details
page = wikipedia.page(result[0])

# Get the summary
summary = page.summary

T = tk.Text(win, height=200, width=70)
T.pack()
T.insert(tk.END, summary)

win.mainloop()

输出

运行上面的 python 代码将打印来自维基百科的与“Python 编程”相关的摘要。

更新于:04-Mar-2021

1 千次浏览

开始你的职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.