When I use requests library, I get different status code (error 503) but when I inspect page and lookout for status it shows (error 404).
I tried this:
import requests
print(requests.get("https://www.amazon.de/dp/1015").status_code)
How can I get exact response?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The reason that you are getting different responses is that your browser is sending headers. Headers are extra information about a request. Browsers always send specific headers, and one is the User-Agent header.
If amazon sees this header, it thinks that there is a browser asking for the page, so it is programmed to send a 404 response.
Without this header, it sends a 503 response because it knows you aren’t using a browser. That’s just what it’s programmed to do.
To add headers, use:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0'}
req = requests.get("https://www.amazon.de/dp/1015", headers=headers)
print(req.status_code)
which prints 404.
The User-Agent header tells amazon details about your browser and computer. To find your own User-Agent header, look at the headers in the Network part of your browser’s inspector tab.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0