为什么要尝试:例外:在python Wikipedia API的特定代码行中不起作用?

问题描述 投票:-3回答:1

我正在尝试执行从我的较大代码中删除的非常短的代码,因为我遇到了问题。我能够重现该问题(它给出了完全相同的错误)。

背景信息:我只是想从Wikipedia搜索中获得第一句话。但是因为我要搜索的单词(kip,意思是荷兰语中的鸡肉)具有多种含义,或者有些错误。我想使用try:来绕过此错误:例外:但无论如何它始终显示错误消息。

这里是似乎无效的代码:

import wikipedia
wikipedia.set_lang('nl')

try: 
 summry = wikipedia.summary('kip', sentences=1)
 print(summry + "\n")
except: 
 print("error")

我曾尝试用以下方式替换:

except wikipedia.exceptions.DisambiguationError:

但它仍然不起作用:(它总是显示错误代码,并且不会降级,然后打印“错误”]

/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py:389: 
GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML 
parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on 
another system, or in a different virtual environment, it may use a different parser and behave 
differently.

The code that caused this warning is on line 389 of the file 
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this 
warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.

lis = BeautifulSoup(html).find_all('li')
error

我正在使用repl.it对此进行编程

如果有人对为什么它仍然显示错误有任何想法,请告诉我:D

我正在尝试执行从我的较大代码中删除的非常短的代码,因为我遇到了问题。我能够重现该问题(它给出了完全相同的错误)。背景...

python wikipedia-api
1个回答
0
投票

首先,谢谢大家的评论:D很有帮助

最后,对我有用的解决方案灵感来自Askold Ilvento的评论

© www.soinside.com 2019 - 2024. All rights reserved.