Py's wikipedia-api: Detailed guide to the introduction, installation and usage of wikipedia-api
Table of contents
1. Create Wikipedia and query it
Introduction to wikipedia-api
Wikipedia-API is an easy-to-use Python wrapper for accessing Wikipedia's API. It supports extracting text, chapters, links, categories, translations and other information from Wikipedia. The documentation provides code snippets for the most common use cases.
Installation of wikipedia-api
pip install -i https://mirrors.aliyun.com/pypi/simple wikipedia-api
How to use wikipedia-api
1、 创built Wikipedia并进行查询
import wikipediaapi
# 创建 Wikipedia API 的实例并设置用户代理
wiki_wiki = wikipediaapi.Wikipedia('MyProjectName ([email protected])', 'en')
# 定义要查询的主题
search_topic = "Artificial Intelligence"
# 获取页面对象
page_py = wiki_wiki.page(search_topic)
# 检查页面是否存在
if page_py.exists():
print("Page - Title:", page_py.title)
print("Page - Summary:", page_py.summary[:60]) # 输出摘要的前60个字符
else:
print("Page not found.")