我正在使用的API:https://pypi.python.org/pypi/python-amazon-simple-product-api
我有我的代码的这部分
from amazon.api import AmazonAPI
products = self.amazon_api.search_n(1, Keywords=item['upc'], SearchIndex='All')
我得到了例外
SearchException: Amazon Search Error: 'AWS.ECommerceService.NoExactMatches', 'We did not find any matches for your request.'
我试着像这样抓住它
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
但我得到了
NameError: global name 'SearchException' is not defined
然后我在脚本开始时这样做了
from AWS.ECommerceService.NoExactMatches import SearchException
但后来我收到了这个错误
ImportError: No module named AWS.ECommerceService.NoExactMatches
我的问题是如何静默捕获这个特定的SearchException
例外?
答案 0 :(得分:5)
试试这个:
from amazon.api import AmazonAPI, SearchException
之后,
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
应该按预期工作。
可以找到文档here。
答案 1 :(得分:-1)
尝试NoExactMatches
例外:
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