[英]python requests .status_code not returning correct value
Looking at the documentation here: http://docs.python-requests.org/en/latest/user/quickstart/ 查看此处的文档: http : //docs.python-requests.org/en/latest/user/quickstart/
This should print 200
and it does. 这应该打印
200
,它确实。
import requests
r = requests.get('http://souke.xdf.cn/Category/1-40-0-0.html?v=5&page=1&pagesize=50')
print r.status_code
This should print 404
but it prints 200
这应该打印
404
但它打印200
import requests
r = requests.get('http://souke.xdf.cn/CategoryXXX/1-40-0-0.html?v=5&page=1&pagesize=50')
print r.status_code
Why is that? 这是为什么?
Is there another way to recognize a 404
error has occurred? 有没有另一种方法来识别
404
错误发生了?
The problem isn't with requests
but with the site you're accessing. 问题不在于
requests
而在于您正在访问的站点。 It's returning 200
. 它回归
200
。
You can confirm this by looking at the headers using something like the Chrome developer tools: 您可以使用Chrome开发人员工具查看标题来确认这一点:
Request URL:http://souke.xdf.cn/CategoryXXX/1-40-0-0.html?v=5&page=1&pagesize=50
Request Method:GET
Status Code:200 OK
The page you are looking for is found on the server , therefore the server responded with a 200 OK. 您正在寻找的页面位于服务器上,因此服务器响应200 OK。 Nevertheless you can use Requests's raise_for_status() , to raise an exception whenever a server error is found, like 404 , 401 and so on.
不过,您可以使用Requests的raise_for_status(),在发现服务器错误时引发异常,例如404,401等。
import requests
>>>>r = requests.get('http://something.com/404/')
>>>>print r.status_code
404
>>>>r.raise_for_status()
Traceback (most recent call last):
File "requests/models.py", line 832, in raise_for_status
raise http_error
requests.exceptions.HTTPError: 404 Client Error
.raise_for_status()
this will raise error if not 200 如果不是200,这将引发错误
this is better than using 这比使用更好
.status_code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.