维基百科消歧错误 [英] Wikipedia disambiguation error

查看:39
本文介绍了维基百科消歧错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用维基百科模块来确定一个随机的维基百科页面.

I have recently been using the wikipedia module to determine a random wikipedia page.

我一直在用一个非常大的单词列表和 random.choice() 模块来做这个:

I have been doing this with a very large list of words, and the random.choice() module as so:

words=open("words.txt","r")
words=words.read()

words=words.split()    

text=random.choice(words)

string=random.choice(wikipedia.search(text))

p = wikipedia.page(string)

系统似乎最常工作,但偶尔会出错:

The system appears to most often work, but will occasionally choke out the error:

Traceback (most recent call last):
  File "/home/will/google4.py", line 25, in <module>
    p = wikipedia.page(string)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 276, in page
    return WikipediaPage(title, redirect=redirect, preload=preload)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 299, in __init__
    self.__load(redirect=redirect, preload=preload)
  File "/usr/local/lib/python2.7/dist-packages/wikipedia/wikipedia.py", line 393, in __load
    raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)
DisambiguationError: "The Scarf" may refer to: 
The Scarf (film)
The Scarf (opera)
Scarf (disambiguation)
Arthur Stewart King Scarf  

无论如何我可以绕过它?

Is there anyway by which I can bypass this?

推荐答案

您可以捕获 DisambiguationError 并随机选择其中一个页面.

You could catch the DisambiguationError and chose one of these pages randomly.

try:
    p = wikipedia.page(string)
except wikipedia.DisambiguationError as e:
    s = random.choice(e.options)
    p = wikipedia.page(s)

见这里:http://wikipedia.readthedocs.io/en/latest/quickstart.html

这篇关于维基百科消歧错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆