1

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?

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I see, then how do I get python to recognize that it is supposed to be a 404 error? Is there another method?
@jason_cant_code There's no method that will do it because request isn't doing anything wrong: the remote server is. So the best solution would be to ask the site to fix their server. Otherwise, you're stick with creating site-specific heuristics, like parsing the request content and looking for '404' in the title.
0

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

Comments

0
.raise_for_status()

this will raise error if not 200

this is better than using

.status_code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.