DEV Community

Muhammad Ichsanul Fadhil
Muhammad Ichsanul Fadhil

Posted on • Updated on

Fetch linkedin data user easily with linkedin api & python

In this article i will show to you how fetch a linkedin user's profile an anything related to data linkedin user with Linkedin API and Python.

So let's go 😄

Setup

Before start in coding we must install the depedency and other tool, there are is.

  • Python >= 3.6
  • pip3

If you have done with both of tool next we can install the package with this command.

pip3 install linkedin-api~=2.0.0a
Enter fullscreen mode Exit fullscreen mode

So the library was succesfully installed and now we can use them.

Add package and credentials

First we must fill the credentials account for login, and dont remember to import the package into our python file.

from linkedin_api import Linkedin

linkedin = Linkedin("youremaillinkedin", "yourpasslinkedin")

Enter fullscreen mode Exit fullscreen mode

Now variable linkedin will become to object that can we use for any action with in.

Uses

So lets with simple example.

Get all user's information

from linkedin_api import Linkedin

linkedin = Linkedin("youremail", "yourpassword")
profile = linkedin.get_profile("billy-g")

print(profile)
Enter fullscreen mode Exit fullscreen mode

Get only particular item

from linkedin_api import Linkedin

linkedin = Linkedin("youremail", "yourpassword")
profile = linkedin.get_profile("billy-g")

print(profile["summary"])
print(profile["firstName"])
Enter fullscreen mode Exit fullscreen mode

Get more user linkedin data and import to csv

import csv
from linkedin_api import Linkedin

# field header for csv
field_names = ['No', 'Name', 'Email', 'Contact','Address']

dataUsers = []

# users linkedin
# add your linkedin user that want you scrape here
usersLinkedin = ["aulina-indah","muhammad-ichsanul-fadhil"]

# increment number
no = 0

# do scrape
linkedin = Linkedin("ich***l67@gmail.com", "***")

for user in usersLinkedin:
    no += 1

    profile = linkedin.get_profile(user)
    profileContact = linkedin.get_profile_contact_info(user)

    userTemp = {
        'No': no,
        'Name': profile['firstName'] + ' ' + profile['lastName'],
        'Email': profileContact['email_address'],
        'Contact' : profileContact['phone_numbers'][0]['number']
        'Address' : profile['geoLocationName']
    }

    dataUsers.append(userTemp)

with open('linkedin.csv', 'w', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames = field_names)
    writer.writeheader()
    writer.writerows(dataUsers)
Enter fullscreen mode Exit fullscreen mode

and more.....

You can find any more api at https://linkedin-api.readthedocs.io/en/latest/api.html

Thank you 😉

Top comments (0)