如何在 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 千次瀏覽

開始你的職業生涯

完成課程獲取認證

開始
廣告