user2548635 user2548635 - 1 year ago 149
Python Question

Python urllib2.HTTPError: HTTP Error 503: Service Unavailable on valid website

I have been using Amazon's Product Advertising API to generate urls that contains prices for a given book. One url that I have generated is the following:

When I click on the link or paste the link on the address bar, the web page loads fine. However, when I execute the following code I get an error:

url = ""
html_contents = urllib2.urlopen(url)

The error is urllib2.HTTPError: HTTP Error 503: Service Unavailable. First of all, I don't understand why I even get this error since the web page successfully loads.

Also, another weird behavior that I have noticed is that the following code sometimes does and sometimes does not give the stated error:

html_contents = urllib2.urlopen("")

I am totally lost on how this behavior occurs. Is there any fix or work around to this? My goal is to read the html contents of the url.


I don't know why stack overflow is changing my code to change the amazon link I listed above in my code to rads.stackoverflow. Anyway, ignore the rads.stackoverflow link and use my link above between the quotes.

Ben Ben
Answer Source

It's because Amazon don't allow automated access to their data, so they're rejecting your request because it didn't come from a proper browser. If you look at the content of the 503 response, it says:

To discuss automated access to Amazon data please contact For information about migrating to our APIs refer to our Marketplace APIs at, or our Product Advertising API at for advertising use cases.

This is because the User-Agent for Python's urllib is so obviously not a browser. You could always fake the User-Agent, but that's not really good (or moral) practice.

As a side note, as mentioned in another answer, the requests library is really good for HTTP access in Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download