wikipedia使用指南

https://pypi.org/project/wikipedia/

https://github.com/goldsmith/Wikipedia

https://www.codenong.com/s-getting-started-with-pythons-wikipedia-api/

安装

pip install wikipedia 

根据title搜索维基百科文章

search()方法在Wikipedia中搜索作为其参数提供的查询,返回包含该查询的所有文章标题的列表。

import wikipedia 
print(wikipedia.search("Bill"))
# ['Bill', 'The Bill', 'Bill Nye', 'Bill Gates', 'Bills, Bills, Bills', 'Heartbeat bill', 'Bill Clinton', 'Buffalo Bill', 'Bill & Ted', 'Kill Bill: Volume 1'] 

print(wikipedia.search("Bill", results=2))
# ['Bill', 'The Bill']

suggest()方法返回与作为参数输入的搜索查询相关的建议,如果找不到建议,它将返回" None"。

import wikipedia
print(wikipedia.suggest("Bill cliton"))
# bill clinton
# 输入了错误的" Bill clinton",并返回了正确的建议" bill clinton"

提取维基百科文章摘要

使用summary()方法提取Wikipedia文章的摘要。

print(wikipedia.summary("Ubuntu"))  # 提取" Ubuntu"的摘要
"""
Ubuntu ( (listen)) is a free and open-source Linux distribution based on Debian. Ubuntu is officially released in three editions: Desktop, Server, and Core (for the internet of things devices and robots). Ubuntu is a popular operating system for cloud computing, with support for OpenStack.Ubuntu is released every six months, with long-term support (LTS) releases every two years. The latest release is 19.04 ("Disco Dingo"), and the most recent long-term support release is 18.04 LTS ("Bionic Beaver"), which is supported until 2028. Ubuntu is developed by Canonical and the community under a meritocratic governance model. Canonical provides security updates and support for each Ubuntu release, starting from the release date and until the release reaches its designated end-of-life (EOL) date. Canonical generates revenue through the sale of premium services related to Ubuntu. Ubuntu is named after the African philosophy of Ubuntu, which Canonical translates as"humanity to others" or"I am what I am because of who we all are".
"""

通过配置方法的sentences参数,我们可以自定义要显示的摘要文本中的句子数。

print(wikipedia.summary("Ubuntu", sentences=2))
# Ubuntu ( (listen)) is a free and open-source Linux distribution based on Debian. Ubuntu is officially released in three editions: Desktop, Server, and Core (for the internet of things devices and robots).

如果页面不存在或页面是明确的,则wikipedia.summary将引发"歧义消除错误"。

print(wikipedia.summary("key"))  # 抛出DisambiguationError,因为有许多文章与" key"匹配。

通过更具体的查询,可以在输出中获得正确的摘要。

print(wikipedia.summary("Key (cryptography)"))

维基百科页面对象

为了获取Wikipedia页面的内容,类别,坐标,图像,链接和其他元数据,我们必须首先获取Wikipedia页面对象或页面的页面ID,可以通过page()方法来获取。

wikipedia.page("Ubuntu")  # 返回一个WikipediaPage对象

然后调用该类的函数或者变量来获取关于文章的更多信息。

print(wikipedia.page("Python").content)  # 完整纯文本内容(不包括图像,表格等)
print(wikipedia.page("Python").url)  # 页面的URL
print(wikipedia.page("Python").references)  # 外部链接的URL
print(wikipedia.page("Python").title)  # 提取页面标题
print(wikipedia.page("Python").categories)  # 页面的类别列表
print(wikipedia.page("Ubuntu").links)  # 获取页面中存在链接的页面标题列表
根据坐标查找页面

geosearch()方法根据提供的坐标返回文章。

print(wikipedia.geosearch(37.787, -122.4))
"""
['140 New Montgomery', 'New Montgomery Street', 'Cartoon Art Museum', 'San Francisco Bay Area Planning and Urban Research Association', 'Academy of Art University', 'The Montgomery (San Francisco)', 'California Historical Society', 'Palace Hotel Residential Tower', 'St. Regis Museum Tower', 'Museum of the African Diaspora']
"""

同样,我们可以设置page()的坐标属性,并获取与地理位置有关的文章。

print(wikipedia.page(37.787, -122.4))
"""
['140 New Montgomery', 'New Montgomery Street', 'Cartoon Art Museum', 'San Francisco Bay Area Planning and Urban Research Association', 'Academy of Art University', 'The Montgomery (San Francisco)', 'California Historical Society', 'Palace Hotel Residential Tower', 'St. Regis Museum Tower', 'Museum of the African Diaspora']
"""
语言设定

通过使用set_lang()方法可以将Wikipedia页面的语言自定义为某种语言。 每种语言都有一个标准的前缀代码,该代码作为参数传递给该方法。

wikipedia.set_lang("de")  
print(wikipedia.summary("ubuntu", sentences=2))  # 以德语获取" Ubuntu" Wiki页面的摘要文本的前两个句子。
"""
Ubuntu (auch Ubuntu Linux) ist eine Linux-Distribution, die auf Debian basiert. Der [Name](http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+name) Ubuntu bedeutet auf Zulu etwa ?Menschlichkeit" und bezeichnet eine afrikanische Philosophie.
"""
print(wikipedia.languages())  # 检查当前支持的ISO语言列表及其前缀。
检索维基百科页面中的图像和完整HTML页面内容

从Wikipedia页面获取图像。

print(wikipedia.page("ubuntu").images[0])  #  从Wikipedia的Ubuntu页面返回第一个图像的链接

获取HTML格式的完整Wikipedia页面。

print(wikipedia.page("Ubuntu").html())
"""
<div class="mw-parser-output"><div role="note" class="hatnote navigation-not-searchable">For the African philosophy, see <a href="/wiki/Ubuntu_philosophy" title="Ubuntu philosophy">Ubuntu philosophy</a>. For other uses, see <a href="/wiki/Ubuntu_(disambiguation)" class="mw-disambig" title="Ubuntu (disambiguation)">Ubuntu (disambiguation)</a>.</div> <div class="shortdescription nomobile noexcerpt noprint searchaux" style="display:none">Linux distribution based on Debian</div> ...
"""
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值