Reputation: 101
I'm trying to figure out how to check if a Wikipedia article exists. For example,
https://en.wikipedia.org/wiki/Food
exists, however
https://en.wikipedia.org/wiki/Fod
does not, and the page simply says, "Wikipedia does not have an article with this exact name."
Thanks!
Upvotes: 9
Views: 3193
Reputation: 1053
You can use Wikipedia Api for Python and just use the keyword to search the article.It also suggest you the closely related available articles. Check the below example
>>> import wikipedia as wiki
>>> wiki.search("Barack")
[u'Barak (given name)', u'Barack Obama', u'Barack (brandy)', u'Presidency of Barack Obama', u'Family of Barack Obama', u'First inauguration of Barack Obama', u'Barack Obama presidential campaign, 2008', u'Barack Obama, Sr.', u'Barack Obama citizenship conspiracy theories', u'Presidential transition of Barack Obama']
>>> wiki.search("Ford", results=3)
[u'Ford Motor Company', u'Gerald Ford', u'Henry Ford']
Here is the link for python module.
Upvotes: 4
Reputation: 1439
Basicly, most website or web service will announce some status from each your HTTP request in the HTTP response header.
In your case, you can simply find the status code if is 404 while the article is not existed even though your brower rendered a page like a normol result.
import request
result = request.get('https://en.wikipedia.org/wiki/Food')
if result.status_code == 200: # the article exists
pass # blablabla
Upvotes: 3
Reputation: 76
Even though Wikipedia does deliver a page, if you look at its request & response data, you will see:
Post Python 2.6, you can use
import urllib
urllib.urlopen("https://some-url").getcode()
to return the status code of that request to test in your code.
Upvotes: 2
Reputation: 560
>>> import urllib
>>> print urllib.urlopen("https://en.wikipedia.org/wiki/Food").getcode()
200
>>> print urllib.urlopen("https://en.wikipedia.org/wiki/Fod").getcode()
404
is it ok?
or
>>> a = urllib.urlopen("https://en.wikipedia.org/wiki/Fod").getcode()
>>> if a == 404:
... print "Wikipedia does not have an article with this exact name."
...
Wikipedia does not have an article with this exact name.
Upvotes: 8