2

I just started to learn Python and faced with this problem. Trued to parse price from amazon and print it to console.

This is my code:

import requests, bs4

def getAmazonPrice(productUrl):
    res = requests.get(productUrl)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    elems = soup.select('#addToCart > a > h5 > div > div.a-column.a-span7.a-text-right.a-span-last > span.a-size-medium.a-color-price.header-price')
    return elems[0].text.strip()


price = getAmazonPrice('http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book')
print('The price is ' + price)

Error Message:

Traceback (most recent call last): File "D:/Code/Python/Basic/webBrowser-Module.py", line 37, in price = getAmazonPrice('http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book') File "D:/Code/Python/Basic/webBrowser-Module.py", line 30, in getAmazonPrice res.raise_for_status() File "C:\Python33\lib\requests\models.py", line 844, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url: http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book

Process finished with exit code 1

1
  • I get the same error message. __requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url __ even after doing as suggested Commented May 26, 2021 at 10:51

1 Answer 1

7

Pretending to be a real browser by providing a User-Agent header would fix this particular issue:

res = requests.get(productUrl, headers={
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36"
})

You would also need to tweak your CSS selector. For instance, .header-price would get you all of the prices on the page (non-prime and prime in this case).

Sign up to request clarification or add additional context in comments.

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.