-2

i am trying to execute this very short code that took out from my bigger code because i was having problems with it. I was able to reproduce the problem (it gave the exact same error).

BACKGROUND INFO: i am just trying to get the first sentence out of this wikipedia search. but because the word i am searching for (kip, means chicken in Dutch) has a wide variety of meanings or something i get an error. i want to bypass this error using try: except: but it keeps displaying the error message anyway.

here is the code that just doesnt seem to work:

import wikipedia
wikipedia.set_lang('nl')

try: 
 summry = wikipedia.summary('kip', sentences=1)
 print(summry + "\n")
except: 
 print("error")

i have tried replacing except: with this

except wikipedia.exceptions.DisambiguationError:

but it still doesnt work :( it always displays the error code regradless and prints "error" afterwards

/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py:389: 
GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML 
parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on 
another system, or in a different virtual environment, it may use a different parser and behave 
differently.

The code that caused this warning is on line 389 of the file 
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this 
warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.

lis = BeautifulSoup(html).find_all('li')
error

i am using repl.it to program this

if anyone has any idea about why it keeps displaying the error anyway please please let me know :D

FireHawkss
  • 11
  • 3
  • It doesn't seem to be an issue with your code, it seems like an issue with the library. Specifically when searching for the 'kip' page. I changed `'kip'` to `'wikipedia'` and it runs no problem. – kennyvh May 25 '20 at 21:47
  • It does catch the error. It displays some warning. You may try to suppress the warning with `with warnings.catch_warnings(): warnings.simplefilter("ignore")`. – Askold Ilvento May 25 '20 at 21:49
  • 1
    first you should use `except Excption as ex: print(ex)` (or run code without try/except) to see what really is the problem. The rest is only warning about argument in `BeautifulSoup` - but it is only warning, not error, so it can't raise error which you catch. – furas May 25 '20 at 21:59
  • Looking at the GitHub repo, it seems like this issue was addressed by a [commit](https://github.com/goldsmith/Wikipedia/commit/50bc236836dc20546af61ea7ca6198c3f039a816), but the API is no longer maintained by the developer from the looks of it. – Jake Tae May 25 '20 at 22:01
  • my question got 3 downvotes. can i ask why? i genuienly want to know what i did wrong :D so i dont do the same mistake again. was the question structured badly or something? – FireHawkss May 26 '20 at 10:45

1 Answers1

1

First of all, thank you all for commenting :D it helped a lot

At the end the solution that worked for me was inspired from Askold Ilvento's comment

although

with warnings.catch_warnings(): warnings.simplefilter("ignore")

didn't work when i adapted it a little bit it did the job!

this is the code that solved the problem and allowed me to ignore what was actually a warning (not an exception) and stop displaying it

import warnings

warnings.catch_warnings()
warnings.simplefilter("ignore") 

i just added this at the start of the script and it solved the problem :D once again all the credit for this code goes to Askold Ilvento i just had to add a little bit to it to make it work

FireHawkss
  • 11
  • 3