我正在努力避免在运行维基百科API时遇到的错误,并且我有一个消歧内容要搜索,通常只会得到类似的维基百科标题,但现在我得到了这个异常wikipedia.exceptions.DisambiguationError(title, may_refer_to)
,
Traceback (most recent call last):
File "/Users/nicolas/Desktop/copia 2.py", line 303, in <module>
output1 =(wikipedia.summary(hulk, sentences=2))
File "build/bdist.macosx-10.12-intel/egg/wikipedia/util.py", line 28, in __call__
File "build/bdist.macosx-10.12-intel/egg/wikipedia/wikipedia.py", line 231, in summary
File "build/bdist.macosx-10.12-intel/egg/wikipedia/wikipedia.py", line 276, in page
File "build/bdist.macosx-10.12-intel/egg/wikipedia/wikipedia.py", line 299, in __init__
File "build/bdist.macosx-10.12-intel/egg/wikipedia/wikipedia.py", line 393, in __load
wikipedia.exceptions.DisambiguationError: "Hulk" may refer to:
Hulk (comics)
Hulk (film)
Hulk (video game)
Hulk Hogan
Antonio Salazar (footballer)
Hulk (footballer)
Johannes Frederik Hulk
Hulk (ship type)
Hulk (medieval ship type)
Prison hulk
De Hulk
The Incredible Hulk (disambiguation)
这会停止程序,我想避免错误并只获得不同标题的打印,例如“Hulk(漫画)(完整列表)
答案 0 :(得分:1)
Wikipedia API quickstart document有一个建议:捕获异常并提取所需的文本:
try:
mercury = wikipedia.summary("Mercury")
except wikipedia.exceptions.DisambiguationError as e:
print e.options
[u'Mercury (mythology)', u'Mercury (planet)', u'Mercury (element)', u'Mercury, Nevada', ...]