If you haven’t read Part 1, please do so first.
Download this extension to boost your presence on LinkedIn.
Analytical Tool that reflects the count of followers of LinkedIn Hashtags to enhance your posts for better reach.
So what did I exactly do?
To obtain the data into my Spreadsheet, I considered exploiting the power of Google Apps Script’s integration with Sheets to interact with the outside world and append data onto new rows. To access Apps Script from the Spreadsheet, choose Tools -> Script Editor. It will open a new tab, with the code editor waiting for you to write your code. The simple function I wrote handled the basic task of calling an API, get the data, parsing it, and then finally appending it at the end of my spreadsheet. The script looks something like this:-
- Line 1 defines our function name.
- Line 2 & 3 do the job of calling the API by sending the post id as a parameter in the request URL and storing the returned values in content.
- Line 5 gets the current Date & Time and stores it in a variable called now.
- Line 6 and 7 help to append the retrieved data into the next row of my current spreadsheet.
Building the API
To run the script, we first need to build our API, which performs the essential part of scraping the LinkedIn website and obtaining the relevant data. For this, we create a simple Flask application, as shown below, and understand it line by line.
- Line 1– 4 import the necessary modules required for us to run the script.
- Line 6 generates an instance of the class Flask in your main module.
- Line 7 and 8 are used to set cookies, which will be sent later to the server along with our request. We will discuss later why we need them and how we can retrieve them.
- On line 10, we define our function, which does the work of obtaining data from the LinkedIn servers. We get the HTML code of our particular post and find the respective views, likes, and comments using basic python functionalities.
- On line 25, we create a flask decorator, specifying the route and methods it expects. It calls our get_data function and sends back jsonifyied data to the user, whenever he calls the API with the unique post id.
- Lines 33,34 means that, whenever we execute our file, run the flask application server.
If you want to run this application locally, you can do it by first installing Python and then flask on your computer. Then copy the above code and save it in a file called app.py. Next, open up the terminal and navigate to the folder where you stored this file and run the Flask application with the command python app.py Now once the server is running, you can open up a browser and type in a URL localhost:5000?id=<Post_id> You should get something like this:
At this point, we are ready to host our application onto a server on the internet so we can fetch data using a URL directly from the Google script we wrote. For this, I will recommend using Heroku, but you can use any other server of your choice available. To upload your application to Heroku, you can watch this simple tutorial. Once you have uploaded the code on the server and obtained the domain URL, replace the URL in the Google script with the post-id, and run it, you will see the data extracted from LinkedIn directly in the Sheet implying our script is running successfully.
So how did I automate this process?
A wonderful feature by Google Scripts is something called Triggers. As the name suggests, Triggers let Apps Script run a function automatically when a certain event occurs. We can install our triggers by choosing Edit -> Current project’s triggers from the script editor. Now, if we click the little plus sign at the bottom right of the screen, a pop-up will appear. You can set the desired time interval at which you want to execute the script and click save.
At this point, we are all done and now you can also create a simple Analytic tool using Google Sheets for LinkedIn. I would love to see the results you all get using it. Also, if you have any problems in setting this up or have any suggestions, feel free to contact me on LinkedIn.
Why do we need cookies and how to get them?
A cookie is a small piece of data sent from a website and stored on the user’s computer by the user’s web browser to individually identify him. Thus, when you visit the website next time, it already knows who is accessing it.
Therefore we will use the li_at cookie which LinkedIn stores on our computer to obtain views, likes, and comments on our post. To get this cookie first log in to your LinkedIn account, next right-click anywhere on the screen and select Inspect Element or Inspect. This will open the Developers Toolbox, here, select the Application/Storage tab and search for cookies. Once there, you should find a cookie named li_at. Copy and past it in the app.py file.
Important Note — Never share this cookie publically or upload it on GitHub as anyone with the cookie can log into your LinkedIn account and use it. Therefore, always use it as an environment variable on the server.
How to find the Post-id on Linkedin?
Post-id is a unique number that LinkedIn assigns to your posts and is found in the URL when you click the share option on the post. This can access the particular post you want to keep track of and can be found in the URL as seen in the image below.
You can connect with me on LinkedIn for more awesome posts and updates. Thanks for reading!
Chaitanya Arora posted on LinkedIn
Today, I was cleaning my #google Drive and came across some folders which were shared by someone else. I wanted to copy…