Python Is The Sure Way To Make Money (Part 3)
“Telegram is python’s best business partner”
Python is becoming more and more popular as a tool for making money. This is consistent with what we discussed in Part one of this article for various reasons. Part two of this article’s only 16 lines of code demonstrated how easy it is to scrape website data with Python.
Part three, the final section of this article, will discuss the best business partner for a Python programmer, e.g., Telegram. We don’t have to wait for jobs through freelancer websites or other means; instead, with Telegram, we can actively create some services for our community and earn money in return.
For example, in Telegram, we can create the following services for our community:
a. Traffic reports: We can scrape some websites that publish traffic reports in real time and forward it to our Telegram bot, which will then forward them to others who have subscribed to our Telegram channel. We can monetize our Telegram channel by posting affiliate marketing products on occasion.
b. URL shortener: We can create url shortener services so that others don’t have to go to url shortener websites but can post their url in our Telegram channel, and our telegram bot powered by Python programming on the Heroku server will shorten it instantly. Maybe we can give away the first ten URLs for free, and then if they want to shorten more URLs, they can pay a meager membership fee, say $1. Telegram offers a payment service that we can use directly, eliminating the need to build a new one.
c. Price tracking: Similar with the second example above. For our community, we can provide price tracking via Telegram. Others only need to post the product url in Telegram, and the python programming in Heroku server will keep track of the product url and send a message to Telegram to notify the person if the product price falls below the desired price. Below, we will attempt to build a simple Python application to demonstrate this.
We will use the Selenium package this time for scraping price data from the Amazon website. Why Selenium? Because for websites generated by java script or websites sensitive to data scraping, Selenium performs better than BeautifulSoup. Selenium use chromedriver, so it will simulate natural browsing, not browsing for scraping purpose.
First, we need to install the Selenium package to our Python environment
Then we will need to get chromedriver from here. Choose the version according to your operating system; for Windows, we can use the 32 version even though we have a 64bit system.
Place chromedriver.exe in our virtual environment’s Scripts folder. As a result, when we use Selenium later, we won’t have to specify the path of chromedriver. So, we are ready to begin coding our application now.
First, we import Selenium’s webdriver and by function. Aside from that, we import the requests package for communicating with the Telegram bot and the Python time module for later use.
Because we are only making a basic price tracking app this time, the input will come directly from our computer, with only the output sent to our Telegram bot. When writing this article, I used https://www.amazon.com/HAVIT-Rainbow-Backlit-Gaming-Keyboard/dp/B016Y2BVKA/ as the product URL.
We save the product URL into variable producturl and our alert price into variable alertprice. To ensure that the variable is filled correctly, we print out the alertprice variable. Remember, for alertprice we want to get the number without a $ sign. Because the input function’s return is a string, we must convert it to a float before comparing it to the current product price. Python’s float type denotes a decimal number rather than a whole number.
Next, we create a function for checking the product price on the Amazon website. Why do we need to create a function? It will be explained in the latter part of this article.
If in Part two, BeautifulSoup has a find function for finding web elements in a website, Selenium has find_element function. In the examples above, we use find_element by ID for finding web elements using its ID attribute and find_element by Class_Name for finding elements using its CLASS attribute. We use get function from webdriver to retrieve the product URL from Amazon website.
If we want to send the alert to our Telegram Bot, we need to change the print function on line eight to our send_message function that we will create below. We can try our application functionality later by changing the alert price to try whether our application sends a notification or not.
How can we get our Bot token and chat ID? Please refer to the Telegram website here. Please don’t forget to change line eight of our coding after we get our Bot token and chat ID to this:
To ensure that we track if Amazon updates the product price on its website, we now need to call our function not only once but periodically using the while function. To avoid overburdening the website, set the sleep time to 3600 seconds during deployment.
In 30 lines of Python code, we were able to create our price tracking app and send the alert notification to our Telegram bot. Hopefully, this will inspire some of us to devise new ways to monetize Python and Telegram. The complete source code can be found below.