Wikipedia disambiguation error

后端 未结 3 1445
独厮守ぢ
独厮守ぢ 2021-01-15 05:09

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

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

3条回答
  •  伪装坚强ぢ
    2021-01-15 05:43

    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)
    

    see here: http://wikipedia.readthedocs.io/en/latest/quickstart.html

提交回复
热议问题