January 22, 2013 

Amazon Kindle Price Alerts

It's no secret Amazon.com's prices change frequently. Sometimes, many times a day. CamelCamelCamel.com is a good place to track the price of items at Amazon. You can see the history and also get an alert when the price falls below a certain point. The only problem is, you can't track digital items such as Kindle Books. I decided to make a Python script that tracks the price of items on Amazon and e-mails you when the price falls below a certain point.

Let's start with all of our import statements

We will use two arguments: the URL and the target price. For example, we would call the script by the following: "amazon.py http://www.amazon.com/A-Brief-History-Time-ebook/dp/B004WY3D0O 10". Which would find if the book is under $10

The magic happens with Beautiful Soup. First, we need to find out what part of the DOM tree we want to parse. In Firefox or Chrome, you can right click the area that is in interest, and read the relevant code. In this case, clicking the price reveals that it is contained in a bold section with a class of "priceLarge"

Next we use an instance of urllib2 to open the page of the product. We create an instance of Beautiful Soup to parse the page. We then use the find function to locate the price. We trim the whitespace, get rid of the dollar sign, and find the decimal value of the price.

We follow the same procedure to find out the title of the item. We find out the title is found in a span with a class "btAsinTitle". Some titles are long, so we shorten the title to 50 characters.

Next, we open a file and store the values. If the price is met, we send an email price alert. The value of csvfile is not shown, but it is where you will save the file on your system.

Here is the send_text(message). It sends a post request to a php page that sends me an email. The php file has to be running on a server that can handle php files. There are many free options for this.

Here is what the php file is running. Note the "?" before php and before the close bracket have been omitted.

Finally, we schedule the script to run every day. Windows has a service called Task Scheduler. Simply click "Create Basic Task...", enter a description, choose how often to run, and select "Start a program". Select your python script and enter the URL and price as the arguments.

Download entrie scrpt....