Hello everyone I am trying to get the URL if pages fetching using Wikipedia module but I am getting error how can I fix this
import wikipedia
results = wikipedia.search("KFC")
for x in results:
print(x)
getlink = wikipedia.page(x).url
print(getlink)
Error I am getting wikipedia.exceptions.PageError: Page id "k2c" does not match any pages. Try another id!
Tried with f strings as f'wikipedia.page({x).url' but didn't get the expected OP
Double Down (sandwich)
wikipedia.page(Double Down (sandwich)).url
Operations of KFC
wikipedia.page(Operations of KFC).url
Expected OP
print(wikipedia.page("History of KFC").url)
https://en.wikipedia.org/wiki/History_of_KFC
CodePudding user response:
As the quickstart says, an error of this kind is thrown if the function page
does not find the page. In this case is a bit strange because it is wikipedia suggestion. Anyway, surround at least that line in a try-except
block to handle the error:
import wikipedia
results = wikipedia.search("KFC")
for x in results:
print(x)
try:
getlink = wikipedia.page(x).url
except wikipedia.exceptions.PageError as e:
print(f'{x} not found.')
print(getlink)
Also, you could try replacing search
method by suggest
method