Python:检查维基百科文章是否存在

标签 python

我正在尝试找出如何检查维基百科文章是否存在。例如,

https://en.wikipedia.org/wiki/Food

但是存在

https://en.wikipedia.org/wiki/Fod 

没有,该页面只是说:“维基百科没有具有该确切名称的文章。”

谢谢!

最佳答案

>>> import urllib
>>> print urllib.urlopen("https://en.wikipedia.org/wiki/Food").getcode()
200
>>> print urllib.urlopen("https://en.wikipedia.org/wiki/Fod").getcode()
404

可以吗?

>>> 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.

关于Python:检查维基百科文章是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606676/

相关文章:

python - 一个大正则表达式是否比一堆小正则表达式更有效?

python - 我想在 python 中编写一个函数,如果所有数字都是偶数,它返回 true

python - 从其他用户的 YouTube 直播中抓取聊天内容

php - 如何将 PHP 对象传递给 Python,反之亦然

python - 使用 fmin_bfgs 进行逻辑回归和 Scipy 优化

python - Virtualenv 无法正常运行/设置。尝试在 Scripts 目录之外调用 python.exe 时出现 'Access denied'

python - Django_Hosts 导入错误

python - 无法在 Python SOAP 调用中传递身份验证参数

python - virtualenv 中的日志记录模块

python - 如何优雅地处理 Ctrl + C 并关闭 Discord Py 机器人?