paint-brush
Scraping Information From LinkedIn Into CSV using Pythonby@scrapingdog
16,831 reads
16,831 reads

Scraping Information From LinkedIn Into CSV using Python

by manthanJuly 2nd, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this post, we are going to scrape data from Linkedin using Python and a Web Scraping Tool. We are going to extract Company Name, Website, Industry, Company Size, Number of employees, Headquarters Address, and Specialties.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Scraping Information From LinkedIn Into CSV using Python
manthan HackerNoon profile picture

In this post, we are going to scrape data from Linkedin using Python and a Web Scraping Tool. We are going to extract Company Name, Website, Industry, Company Size, Number of employees, Headquarters Address, and Specialties.

Why this tool? This tool will help us to scrape dynamic websites using millions of rotating residential proxies so that we don’t get blocked. It also provides a captcha clearing facility.

Procedure

Generally, web scraping is divided into two parts:

  • Fetching data by making an HTTP request.
  • Extracting important data by parsing the HTML DOM

Libraries & Tools

  • Beautiful Soup is a Python library for pulling data out of HTML and XML files.
  • Requests allow you to send HTTP requests very easily.
  • Pandas provide fast, flexible, and expressive data structures
  • Web Scraper to extract the HTML code of the target URL.

Setup

Our setup is pretty simple. Just create a folder and install Beautiful Soup & requests. For creating a folder and installing libraries type below given commands. I am assuming that you have already installed Python 3.x.

mkdir scraper
pip install beautifulsoup4
pip install requests
pip install pandas

Now, create a file inside that folder by any name you like. I am using scraping.py.

Firstly, you have to sign up for Web Scraper. It will provide you with 1000 FREE credits. Then just import Beautiful Soup & requests in your file. like this.

from bs4 import BeautifulSoup
import requests
import pandas as pd

What we are going to scrape

We are going to scrape the “about” page of Google from Linkedin.

Preparing the Food

Now, since we have all the ingredients to prepare the scraper, we should make a GET request to the target URL to get the raw HTML data. If you are not familiar with the scraping tool, I would urge you to go through its documentation. We will use requests to make an HTTP GET request. Now Since we are scraping a company page so I have set “type” as company and “linkId” as google/about/. LinkId can be found in Linkedin's target URL.

r = requests.get(‘https://api.scrapingdog.com/linkedin/?api_key=YOUR-API-KEY&type=company&linkId=google/about/').text

This will provide you with an HTML code of those target URLs.

Please use your Scrapingdog API key while making above requests.

Now, you have to use BeautifulSoup to parse the HTML.

soup=BeautifulSoup(r,’html.parser’)
l={}
u=list()

As you can see in the image that the title of the company is stored in class “org-top-card-summary__title t-24 t-black truncate” with tag h1.

So, we’ll use variable soup to extract that text.

try:
   l[“Company”]=soup.find(“h1”,{“class”:”org-top-card-summary__title t-24 t-black truncate”}).text.replace(“\n”,””)
except:
   l[“Company”]=None

I have replaced \n with an empty string.

Now, we will focus on extracting website, Industry, Company Size, Headquarters(Address), Type, and Specialties.

All of the above properties (except Company Size)are stored in class “org-page-details__definition-text t-14 t-black — light t-normal” with tag dd. I will again use variable soup to extract all the properties.

allProp = soup.find_all(“dd”,{“class”:”org-page-details__definition-text t-14 t-black — light t-normal”})

Now, we’ll one by one extract the properties from the allProp list.

try:
 l[“website”]=allProp[0].text.replace(“\n”,””)
except:
 l[“website”]=None
try:
 l[“Industry”]=allProp[1].text.replace(“\n”,””)
except:
 l[“Industry”]=None
try:
 l[“Address”]=allProp[2].text.replace(“\n”,””)
except:
 l[“Address”]=None
try:
 l[“Type”]=allProp[3].text.replace(“\n”,””)
except:
 l[“Type”]=None
try:
 l[“Specialties”]=allProp[4].text.replace(“\n”,””)
except:
 l[“Specialties”]=None

Now, we’ll scrape Company Size.

As, you can see that Company Size is stored in class “org-about-company-module__company-size-definition-text t-14 t-black — light mb1 fl” with tag dd.

try:
 l[“Company Size”]=soup.find(“dd”,{“class”:”org-about-company-module__company-size-definition-text t-14 t-black — light mb1 fl”}).text.replace(“\n”,””)
except:
 l[“Company Size”]=None

Now, I will push dictionary l to list u. And then we’ll create a dataframe of list u using pandas.

u.append(l)
df = pd.io.json.json_normalize(u)

Now, finally saving our data to a CSV file.

df.to_csv(‘linkedin.csv’, index=False, encoding=’utf-8')

We have successfully scraped a Linkedin Company Page. Similarly, you can also scrape a Profile. Please read the docs before scraping a Profile Page.

Complete Code

from bs4 import BeautifulSoup
import requests
import pandas as pd
r = requests.get(‘https://api.scrapingdog.com/linkedin/?api_key=YOUR-API-KEY&type=company&linkId=google/about/').text
soup=BeautifulSoup(r,’html.parser’)
u=list()
 l={}
try:
 l[“Company”]=soup.find(“h1”,{“class”:”org-top-card-summary__title t-24 t-black truncate”}).text.replace(“\n”,””)
except:
 l[“Company”]=None
allProp = soup.find_all(“dd”,{“class”:”org-page-details__definition-text t-14 t-black — light t-normal”})
try:
 l[“website”]=allProp[0].text.replace(“\n”,””)
except:
 l[“website”]=None
try:
 l[“Industry”]=allProp[1].text.replace(“\n”,””)
except:
 l[“Industry”]=None
try:
 l[“Company Size”]=soup.find(“dd”,{“class”:”org-about-company-module__company-size-definition-text t-14 t-black — light mb1 fl”}).text.replace(“\n”,””)
except:
 l[“Company Size”]=None
try:
 l[“Address”]=allProp[2].text.replace(“\n”,””)
except:
 l[“Address”]=None
try:
 l[“Type”]=allProp[3].text.replace(“\n”,””)
except:
 l[“Type”]=None
try:
 l[“Specialties”]=allProp[4].text.replace(“\n”,””)
except:
 l[“Specialties”]=None
u.append(l)
df = pd.io.json.json_normalize(u)
df.to_csv(‘linkedin.csv’, index=False, encoding=’utf-8')
print(df)

Conclusion

In this article, we understood how we can scrape data from Linkedin using proxy scraper Python. As I said earlier you can scrape a Profile too but just read the docs before trying it.

Feel free to comment and ask me anything. You can follow me on Twitter. Thanks for reading and please hit the like button! 👍

Additional Resources

And there’s the list! At this point, you should feel comfortable writing your first web scraper to gather data from any website. Here are a few additional resources that you may find helpful during your web scraping journey:

Previously published at https://www.scrapingdog.com/blog/scrape-data-from-linkedin-using-python