[Python] Tried Getting Product Condition Comments from Amazon API
I researched how to get “product condition comments” from information listed on Amazon Marketplace, so I’ll share the method.
This can also be applied when you want to get other product information.
By the way, this time I changed the itemSearch keyword to “Python Cookbook 2nd Edition” in the article [Python] Tried Using Amazon Product Advertising API and got book condition comments from this book’s marketplace.
The information I want to get is the “condition comment,” which is contained in the XML element ConditionNote. The elements in order from parent to child are as follows:
Therefore, when I wanted to get the information of element ConditionNote, I got all parent elements OfferAttributes and tried to look at ConditionNote, which is a child element of OfferAttributes, one by one from the retrieved data, but I got an error.
Below is the error. The first part could be output, but from the middle it couldn’t be retrieved.
$ python t_offerattributes_fail.py
◆◆◆There is dirt on the edge. The cover shows signs of use. Everything else is fine. We strive for prompt and careful shipping. 【Ships daily except Sundays】
Packed with air cushion and shipped promptly.
Traceback (most recent call last):
File "t_offerattributes_fail.py", line 17, in
print item.conditionnote.contents[0]
AttributeError: 'NoneType' object has no attribute 'contents'
Below is the code for the failed case.
t_offerattributes_fail.py
#coding:utf-8
import sys, codecs
sys.stdout = codecs.lookup('utf_8')[-1](sys.stdout)
from amazon import Amazon
# Want to search for books by keyword!
amazon = Amazon("Your Access Key", "Your Secret Access Key")
xml = amazon.itemSearch("Books", Keywords=u"Python Cookbook 2nd Edition", ResponseGroup="OfferFull", MerchantId="All", Condition="All") # Detailed book sales information (OfferFull)
print amazon.url # Request URL
print unicode(xml, 'utf_8') # Amazon response
# Extract information from XML
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(xml) # Store XML in soup
# Extract only offerattributes element information from soup
for item in soup.findAll("offerattributes"):
print item.conditionnote.contents[0]
# Output contents of conditionnote element under item element
Because there are times when the child element ConditionNote doesn’t exist under element OfferAttributes, it causes an error when item.conditionnote.contents[0] is empty.
Therefore, we should directly get the element ConditionNote.
By changing just the last 4 lines of the above program as follows, I was able to successfully get the product condition comments.
# Extract only conditionnote element information from soup
for item in soup.findAll("conditionnote"):
print item.contents[0]
# Output contents of conditionnote element under item element
Below is the output information:
$ python t_offerattributes_true.py
◆◆◆There is dirt on the edge. The cover shows signs of use. Everything else is fine. We strive for prompt and careful shipping. 【Ships daily except Sundays】
Packed with air cushion and shipped promptly.
Brand new unread item. Stock guaranteed. Will ship promptly.
This is a brand new unread book. Carefully packed with waterproofing and shipped promptly. (Within 3 business days in principle) Orders will be promptly canceled if out of stock. Prices may be higher than retail, so please check the retail price. (Closed on Saturdays, Sundays, and holidays in principle.)
Brand new unread item, so it's very clean. We respond promptly and carefully. International shipping available.
★First edition. Cover has very small creases. Interior text is clean. Packed with air cushion and shipped promptly.★
Brand new beautiful item. Vinyl packed and shipped promptly via mail. (Depending on when you order, shipping is next day to one week) Please understand if out of stock. Rare item, so price is slightly higher than retail, but please consider.
Treated as practically new item, sealed in vinyl immediately after purchase and kept unread. However, since it was once shelved or redistributed, condition is listed as "Very Good" for this listing. Please excuse any scuffs from display or shipping. Price is set higher than reference price (4830 yen close to retail) according to market conditions. Please consider carefully before purchasing. Inventory is shared so may be out of stock. In that case, orders will be canceled but please understand. Please order only if you don't mind stock shortages and high pricing.
Using this, it seems possible to apply detailed filters to product searches.
That’s all.
That’s all from the Gemba.