Wikipedia disambiguation error

后端 未结 3 1441
独厮守ぢ
独厮守ぢ 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

    0 讨论(0)
  • 2021-01-15 05:55

    Better yet, use the tool at your disposal :

    wikipedia.random(pages=1)
    
    Get a list of random Wikipedia article titles.
    
    Note
    
    Random only gets articles from namespace 0, meaning no Category, User talk, or other meta-Wikipedia pages.
    
    Keyword arguments:
    
        pages - the number of random pages returned (max of 10)
    

    (from https://wikipedia.readthedocs.io/en/latest/code.html#api)

    0 讨论(0)
  • 2021-01-15 05:56

    One obvious way would be to download a complete list of Wikipedia page names and use that instead of your word list. That would also be much kinder to Wikipedia's search engine which you don't need to get a random page (and besides, if you want a uniform random page, you mustn't use the search engine).

    A less-good but perhaps easier fix would be for you to simply try/except the DisambiguationError and try again.

    0 讨论(0)
提交回复
热议问题