【发布时间】:2017-04-22 01:23:00
【问题描述】:
我正在使用以下代码返回 URL 的状态
import requests
answer = requests.get('http://www.website.com')
answer.status_code
>>>200
这给了我 200。
但是,网站应该返回 404。
answer.content
>>>b'<html><head>\r\n<title>404 Not Found</title>\r\n</head><body>\r\n<h1>Not Found</h1>\r\n<p>The requested URL index.php was not found on this server.</p>\r\n<hr>\r\n<address>Apache/2.2.22 (Linux) Server at Port <small onclick="document.getElementById(\'login\').style.display = \'block\';">80</small></address>\r\n</body></html><div id="login" style="display:none;"><pre align=center><form method=post>Password: <input type=password name=pass><input type=submit value=\'>>\'></form></pre></div>'
有人能告诉我差异的来源以及如何解决此问题以得到 answer.status_code = 404 作为结果而不是 200 吗?我无法直接访问服务器,但我可以询问管理员。
谢谢!
【问题讨论】:
-
您正在获取的网站可能正在返回状态 200,即使返回的内容包含文本
404 Not Found -
使用带有 -v 选项的 curl 等其他工具来获取所有标题和内容。确认标题不匹配。可能是 404 是在 php 代码中生成的,并且该代码没有正确设置状态代码
标签: python url request http-status-codes