© D. Thiébaut 2005,
modified by
J. Franklin, 11/05, 11/06.
getcopy amazon.pyNext, create and run this program:
import amazon # import amazon.com library
def getBooksWithKeywords( keyword ):
pythonBooks = amazon.searchByKeyword( keyword )
print "Number of books found: %d" % ( len( pythonBooks ) )
for i in range( len( pythonBooks ) ):
print "name: %s" % (pythonBooks[i].ProductName)
print "URL: %s" % (pythonBooks[i].URL)
print "Price: %s" % (pythonBooks[i].OurPrice)
print "ISBN: %s" % (pythonBooks[i].Isbn)
print "Image: %s" % (pythonBooks[i].ImageUrlMedium)
print "\n\n"
def main():
getBooksWithKeywords( raw_input( "keywords in book title? " ) )
|
The amazon.searchByKeyword( keyword ) is one of the functions provided by the amazon.py library. You give it a string representing a keyword, and it interrogates www.amazon.com for a list of books that match this keyword (most likely appearing in the title). The function returns a list of objects that have several instance variables associated with them, such as its ProductName, the url at Amazon where the book is showcased, or the link to the image showing the book cover (ImageUrlMedium). In class we would have defined inspector or accessor methods to lookup all these variables, such as getURL() or for example getX() for the Point objects when using the graphics.py module. Mark Pilgrim decided not to use this method and allows us direct access to the variables. That's fine. That's why we see pythonBooks[i].URL rather than a method call like pythonBooks[i].getURL().
guitarNotice how the formatting in the print statement makes the output look so tidy.
Unexpected crashes
The solution is to make our program aware that there might be exceptions. This is easy to do, and we saw a demonstration of it in class. Because the error we get when the program crashes is defined as an "AttributeError", we can rewrite the statement that causes the program to crash:
print "Price: %s" % (pythonBooks[i].OurPrice)
as follows:
try:
print "Price: %s" % (pythonBooks[i].OurPrice)
except AttributeError:
pass
You can read the code above as follows: "Try to get the attribute OurPrice from
the i-th entry in the list pythonBooks. If successful, print it
and continue. If not successful, and the error encountered is of type AttributeError,
then do nothing (pass), and continue on."
You can use this technique to protect your python statements that will access the list of books, music, or movies that Amazon will return to you. To get more information on the subject, read Section 8.3 of this page from the Python.org site.
import amazon
def getCDsFromArtist( artist ):
musicList = amazon.searchByArtist( artist, product_line="music" )
print "Number of items found: %d" % ( len( musicList ) )
for i in range( len( musicList ) ):
print "name: %s" % (musicList[i].ProductName)
print "URL: %s" % (musicList[i].URL)
print "Price: %s" % (musicList[i].OurPrice)
print "Media: %s" % (musicList[i].Media)
print "Image: %s" % (musicList[i].ImageUrlLarge)
print "Released: %s" % (musicList[i].ReleaseDate)
print "\n\n"
def main2():
getCDsFromArtist( raw_input( "artist? " ) )
|
Rufus WainwrightOops, there goes that AttributeError again! Fix it up with the try-except statement and rerun with the same Artist name.
Rufus Wainwrightyou may now get
AttributeError: Bag instance has no attribute 'ReleaseDate'Put a try-except statement around the print statement for ReleaseDate and continue. You may wish to add try-except to all the prints.
yduJ nilknarFYou should see a long Traceback and this error explanation:
amazon.AmazonError: There are no exact matches for the search.Oops. There were no CDs by artist yduJ nilknarF. The error is amazon.AmazonError.
musicList = amazon.searchByArtist( artist, product_line="music" )to catch this error. What should you do after the except? If you can't figure that out, try running it with the try-except and use the pass statement in the body of the except: part.