質問

Looking at the documentation here: http://docs.python-requests.org/en/latest/user/quickstart/

This should print 200 and it does.

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

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?

役に立ちましたか?

解決

The problem isn't with requests but with the site you're accessing. It's returning 200.

You can confirm this by looking at the headers using something like the Chrome developer tools:

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

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

this is better than using

.status_code
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top