Is there a way to return the length of the results from the following line?
import wikipedia
query = input("What do you want to lookup? ")
wikipedia.search(query)
Thanks!
I know that .search will return the first ten results, and that you can specify more or less, but I'd like to be able to tell how many results there are. I couldn't find this in this documentation, although I may not be looking in the right place. Any help is appreciated! https://wikipedia.readthedocs.io/en/latest/code.html#api
It seems that the wikipedia
package doesn't provide an API for the total number of hits. MediaWiki itself, however, does have such an option: srinfo=totalhits
.
Knowing this, getting the information from Wikipedia should be trivial (the documentation page linked above also has some code examples, Python included):
import requests
response = requests.get('https://en.wikipedia.org/w/api.php', params = {
'action': 'query',
'format': 'json',
'list': 'search',
'srsearch': 'Foobar',
'srinfo': 'totalhits'
}).json()
total_hits = response['query']['searchinfo']['totalhits'] # 131