Search code examples
pythonwikipedia-api

Using wikipedia module in python


I am using wikipedia module in my python code. I would like to have an input from user to search from wikipedia and get 2 lines from its summary. Since there might be many topics with same name, I used like this.

import wikipedia
value=input("Enter what u want to search")
m=wikipedia.search(value,3)
print(wikipedia.summary(m[0],sentences=2))

While executing this its showing some 3 pages of exceptions. Whats wrong with this? Edit: As suggested by @ Ruperto, I changed the code like this.

import wikipedia
import random
value=input("Enter the words: ")
try:
    p=wikipedia.page(value)
    print(p)
except wikipedia.exceptions.DisambiguationError as e:
    s=random.choice(e.options)
    p=wikipedia.summary(s,sentences=2)
    print(p)

Now the error I get is,

Traceback (most recent call last):   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "C:\Users\vdhan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked, urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x03AEEAF0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

What to do now?


Solution

  • It may due to No/Poor internet connection, as your error says,

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
    

    You can change/check your internet conncetion and try again. Neither, it is problem of your python environment. My implementation is,

    import warnings
    warnings.filterwarnings("ignore")
    
    import wikipedia
    import random
    
    
    value=input("Enter the words: ")
    try:
        m=wikipedia.search(value,3)
        print(wikipedia.summary(m[0],sentences=2))
        # print(p)
    except wikipedia.exceptions.DisambiguationError as e:
        s=random.choice(e.options)
        p=wikipedia.summary(s,sentences=2)
        print(p)
    

    Output:

    Enter the words: programming
    Program management or programme management is the process of managing several related projects, often with the intention of improving an organization's performance. In practice and in its aims, program management is often closely related to systems engineering, industrial engineering, change management, and business transformation.
    

    It works fine in google colab, my implementation colab file you can find here.