2

API I am using: https://pypi.python.org/pypi/python-amazon-simple-product-api

I have this part of my code

from amazon.api import AmazonAPI
products = self.amazon_api.search_n(1, Keywords=item['upc'], SearchIndex='All')

Exception I got

SearchException: Amazon Search Error: 'AWS.ECommerceService.NoExactMatches', 'We did not find any matches for your request.'

I tried catching it like this

try:
    products = self.amazon_api.search_n(1, Keywords=item['upc'], SearchIndex='All')
    found_match = True
except SearchException:
    logging.warning("No search result found on Amazon for UPC: %s"%(item['upc']))
    found_match = False

But I got

NameError: global name 'SearchException' is not defined

Then I did this in start of my script

from AWS.ECommerceService.NoExactMatches import SearchException

But then I got this error

ImportError: No module named AWS.ECommerceService.NoExactMatches

My question is how do I silently catch this specific SearchException exception?

2 Answers 2

5

Try this:

from amazon.api import AmazonAPI, SearchException

After that,

try:
    products = self.amazon_api.search_n(1, Keywords=item['upc'], SearchIndex='All')
    found_match = True
except SearchException:
    logging.warning("No search result found on Amazon for UPC: %s"%(item['upc']))
    found_match = False

should work as expected.

Docs can be found here.

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

Comments

-1

Try the NoExactMatches exception:

from AWS.EcommerceServiec import NoExactMatches

try:
    products = self.amazon_api.search_n(1, Keywords=item['upc'], 
SearchIndex='All')
    found_match = True
except NoExactMatches:
    logging.warning("No search result found on Amazon for UPC: %s"%(item['upc']))
    found_match = False

1 Comment

Also, modern IDEs (at least PyCharm — definitely) allow you to import anything by just typing the name of class/method/exception/constant etc. and press Alt + Enter. It saves a lot of time.

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.