Python URL Gets方法返回状态200,即使它是404
我使用以下代码返回URL的状态Python URL Gets方法返回状态200,即使它是404,python,url,request,http-status-codes,Python,Url,Request,Http Status Codes,我使用以下代码返回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&
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.content
>>>b'\r\n404找不到\r\n\r\n找不到\r\n在此服务器上找不到请求的URL index.php。\r\n
\r\n缓存/2.2.22(Linux)服务器端口80\r\n密码:'
有人能告诉我这种差异是从哪里来的吗?我如何解决这个问题以得到答案。状态代码=404而不是200?我无法直接访问服务器,但我可以询问管理员
谢谢大家!
重定向和历史
默认情况下,请求将对除HEAD之外的所有谓词执行位置重定向
我们可以使用响应对象的history属性来跟踪重定向
Response.history列表包含为完成请求而创建的响应对象。列表从最早的响应到最近的响应进行排序
例如,GitHub将所有HTTP请求重定向到HTTPS:
>>> r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]
如果您使用的是HEAD,还可以启用重定向:
>>> r = requests.head('http://github.com', allow_redirects=True)
>>> r.url
'https://github.com/'
>>> r.history
[<Response [301]>]
>r=requests.head('http://github.com,允许_重定向=True)
>>>r.url
'https://github.com/'
>>>r.历史
[]
您正在获取的站点可能正在返回状态200,即使返回的内容包含文本404 Not Found
使用其他工具(如curl with-v选项)获取所有标题和内容。以确认标题不匹配。很可能404是在php代码中生成的,而该代码没有正确设置状态代码。这似乎是复制粘贴,这与他的问题有何关系。我们没有理由怀疑重定向是问题的一部分。如果你认为这是不可能的,那你就需要证明这一点
>>> r = requests.head('http://github.com', allow_redirects=True)
>>> r.url
'https://github.com/'
>>> r.history
[<Response [301]>]