<分区>
我如何使用 python 确定给定 url 中的任何内容是否存在于网络中?它可以是 html 页面或 pdf 文件,应该无关紧要。 我试过本页中写的解决方案 http://code.activestate.com/recipes/101276/ 但是当它是 pdf 文件或任何东西时它只返回 1。
<分区>
我如何使用 python 确定给定 url 中的任何内容是否存在于网络中?它可以是 html 页面或 pdf 文件,应该无关紧要。 我试过本页中写的解决方案 http://code.activestate.com/recipes/101276/ 但是当它是 pdf 文件或任何东西时它只返回 1。
最佳答案
您需要检查 HTTP 响应代码。 Python 示例:
from urllib2 import urlopen
code = urlopen("http://example.com/").code
4xx 和 5xx 代码可能意味着您无法从此 URL 获得任何内容。 4xx 状态代码描述客户端错误(如“404 Not found”),5xx 状态代码描述服务器错误(如“500 Internal server error”):
if (code / 100 >= 4):
print "Nothing there."
链接:
关于python - 我如何确定给定网址中是否存在任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1966086/