Amazon Script
Amazon Script
Ah-ha, another interesting question that I love to answer. So, further wasting any time, let's
dive into it.
Chapter 1
THE CONTEXT
Well, first thing first, If you’re an Indian (or any human being), I think you’re familiar with
either one of these:
or…
If so, then congratulations to you. I promise that this code may come very handy to you.
(Don’t worry if you’re not familiar at all, you’ll definitely find this code very interesting).
Now, a bit of myself. I am an average Indian (more specifically, Bengali) with a messy beard,
something just like this:
Now, since Durgapuja ( The biggest festival for Bengali ) is knocking at the door, I badly
need a trimmer to groom myself. Checking various websites for this, I finally stumbled upon
on amazon.
“Ah-ha …. that’s my deal.”, I said to myself. But unfortunately, they haven’t disclosed the
date and price yet. So, the idea came to my mind: It’s better to write a python script such
that when it hits lower than my desired price, I will get a notification mail (or better to
have a push notification) so that the deal is not going to be missed by me.
But before that, let’s see what the current price for the item is?
Hmmm….. 1120. That’s a bit pricy. Anyway, let's dive into chapter 2.
Chapter 2
THE EXECUTION
So, what I did, I just simply change my desired price to 1200 from 1000 in line no. 30 and see
what’s happened:
Meanwhile, my mobile be like…
Voila!….Push Notification works smoothly. But what’s about the email ??
Chapter 3
THE EXPLANATION
So, what did I do so far? First I opened my chrome and went to the product webpage. Now
before web-scrapping, I checked whether I can extract information or not. So, I just press F12
and it looked like this:
Pretty messy huh……Nevermind, just turn on the inspect icon from the top left corner like
this:
Ok, so now just hover to the Product name and price tag:
Ok, so the id for the product name is ProductTitle. Now, let's fire up PYTHON IDLE and
write this simple code importing the BeautifulSoup library for web scrapping like this:
Let’s see what we get:
Now, just follow, how we had extracted the title ID of the product and get the ID for the
price. Check whether it’s priceblock_ourprice or not. Now just implement it with our
previous code and print whether it works or not by adding these two lines:
1. price = int(soup.find(id="priceblock_ourprice").get_text())
But, since we have our unexpected guests with us, we would get an error like this:
So, we need a little bit of manipulation. That has been done from line no 51 to 63.
The rest is so simple. I have created an SMTP server for sending emails if the criteria (i.e
amazon_price < = my_desired_price) is met and similarly a push notification sending system
has also been built. Now, the question comes, Will it check the price every time OR only
when I run the code? Now, at the very beginning, as I have mentioned that, I don’t know
when that auspicious time (the time, when the price will be lowest ) will come, I have to run
it until it meets my goal. Thus I construct a while loop from line no. 131 to 135 like this:
1. while(True):
2. count += 1
3. print("Count : "+str(count))
4. check_price()
5. time.sleep(60)
So, it will run after an interval of 60 seconds for the infinite many times :)
Chapter 4
THE Q&As
When I shared this with my friends today, they asked me a few questions that you may ask
me also. Thus I have created this chapter.
Q. Are you an idiot because you shared the password of your mail?
A. Well, I may be an idiot, but honestly not for these reasons:
Q. Can I just copy-paste the code to my terminal if I want this to work for me?
A. First thing first, to run this code, you need to follow the following steps:
1. First, you have to install a few packages like bs4, requests, smtplib, etc. You
can install them via pip easily using pip install <package name>
2. Oh! forget to mention that: this works only in Python 3.x. So, make sure that
you have installed Python 3 and not Python 2.
3. Now, as I have mentioned, I changed my password. So, this server is not going
to be worked for you. You have to create your own server using your
credentials.
4. Lastly, for push notification, you need to register to notify_run after installing
it via pip. You can do that easily typing notify-run register in your
terminal and then scan the QR code to the smartphone in which you want to
send the push notification.
Final Chapter
THE CONCLUSION