How to make Analysis Tool with Python | Towards Data Science

Since the new policy in October last year, Medium calculates the earnings differently and updates them daily. Before the change, it was based on the number of claps from members and the amount that they themselves clap in general, but now it is based on reading time. Similar to youtube’s algorithm, which is watch time.
The important thing is that it updates daily and you want to know how much have your stories made and how many views you have in the last 30 days.
As an example website for making this simple Analysis Tool, we will take Medium. But you can do it basically with any site out there that has stats you need.
It is a very simple use of Python and you do not need any specific or rather spectacular skills to do this with me. We will go step by step and build everything from the ground up.
Let’s just jump right into it!
There are a few steps when building such a tool and first, we have to see how to get to what we want.This is where we land when we go to Medium’s welcome page.
It is rather simple and we have sign-in/up buttons. We are going to use those in order to login to our profile. We are going to automate this tool in order for it to click, fill out emails, passwords and log us in. After that, we will get to the data we need.
You are going to have to install a ChromeDriver, which is going to enable us to manipulate the browser and send commands to it for testing and after for use.
Open the link and download the file for your operating system. I recommend the latest stable release unless you know what you are doing already.
Next up, you need to unzip that file. I would recommend going into Files and doing it manually by right-clicking and then “Extract here”.
Inside the folder, there is a file called “chromedriver”, which we have to move to a specific folder on your computer.
Open the terminal and type these commands:
sudo su #enter the root modecd      #go back to base from the current locationmv /home/*your_pc_name*/Downloads/chromedriver /usr/local/bin #move the file to the right location
Just instead of *your_pc_name* insert your actual name of the computer.
Once we are done with that, we open the editor. My personal choice is Visual Studio Code. It is straightforward to use, customizable, and light for your computer.
Open a new Project where ever you like and create two new files. This is an example of how mine looks like to help you:
In the VS Code, there is a “Terminal” tab with which you can open an internal terminal inside the VS Code, which is very useful to have everything in one place.
When you have that open, there is few more thing we need to install and that is the virtual environment and selenium for web driver. Type these commands into your terminal.
pip3 install virtualenvsource venv/bin/activatepip3 install selenium
After activating the virtual environment, we are completely ready to go.
We will create it as a class and make functions for it. So let’s start!
Create your tool with any name and start the driver for Chrome.
class MediumBot():  def __init__(self):    self.driver = webdriver.Chrome()
That is all we need to start developing. Now go to your terminal and type:
python -i scrape.py
This command lets us our file as an interactive playground. The new tab of the browser will be opened and we can start issuing commands to it.If you want to experiment you can use the command line instead of just typing it directly to your source file. Just instead of self use bot.
For Terminal:
bot = MediumBot()bot.driver.get('https://medium.com/')
And now for the source code:
self.driver.get('https://medium.com/')
Now we went over to mediums welcome page and what we want next is to log in. We inspect the element (F12 on keyboard) and copy elements XPath.
Right-click in that marked blue section of code and copy by XPath. Next up, we have to make a command to click that button for us.
sign_in = self.driver.find_element_by_xpath('//*[@id="root"]/div/div[4]/div/div/div[3]/div/div[2]/h4/a')sign_in.click()
In single quotes (‘ ’) is my XPath and you have to adjust yours if you are doing other websites.
I saved the XPath to a variable and perform a click() function on it.
I have done 2 types of login for Medium and those are Google and Facebook, you can also choose which method better suits you, but turn off 2-factor-authentication just so this process gets easier.
For the Facebook method, you will select the Login with Facebook button, get its XPath and click it again.
Now we have to input our username and password and we do it by the send_keys() function.
Again, select the text box and now just send a text to that field like this:
email_in = self.driver.find_element_by_xpath('//*[@id="email"]')email_in.send_keys(username)
Do the same for the password and then Log In with click() function.After logging in, we have access to data we want to get to and I wrote two separate functions to get both earnings and views of your stories.
First views:
def stats(self):  self.driver.get('https://medium.com/me/stats')  sleep(3)  total_views = self.driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[3]/div/ul/li[1]/div/div[1]')  print(total_views.text)
And now earnings:
def earnings(self):  self.driver.get('https://medium.com/me/partner/dashboard')  sleep(3)  total_earnings = self.driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[3]/section/div/div[2]/div/div/div/div[3]')  print(total_earnings.text)
In both of these, I use sleep() function, which lets me pause the further execution for a certain amount of time, so sleep(1) will pause for 1 second.You have to import this at the beginning of your code.
Once you are done with extracting data. All you have to do now is create an instance of this tool outside the class and perform a function on it.
bot = MediumBot()bot.login_facebook()
Save that and run the script. Watch the magic happen before your own eyes!
Here is a complete code on my GitHub page:
github.com
Also, you can change the creditentials.py and fill it with your own data in order to log in.
We are done!
If you want to take this further you can also implement some functions like emails sending at a certain goal you reach or extract data for specific stories you want to track your data.
I hope you liked this little tutorial and follow me for more!
Thanks for reading!