Links

Get connections from network

Tags: #linkedin #network #connections #naas_drivers #analytics #csv #html #image #content #plotly
Author: Florent Ravenel
Last update: 2023-07-26 (Created: 2022-03-03)
Description: This notebook extracts your connections from your LinkedIn profile. It generates a dataframe that includes the following fields: the first and last name of the connection ('FIRSTNAME', 'LASTNAME'), the description present below the name on the profile page ('OCCUPATION'), the date when the connection was made ('CREATED_AT'), the URL of the profile ('PROFILE_URL'), the URL of the profile picture ('PROFILE_PICTURE'), the LinkedIn profile id of the connection ('PROFILE_ID'), and the LinkedIn public profile id of the connection ('PUBLIC_ID')."
Disclaimer: This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by Linkedin or any of its affiliates or subsidiaries. It uses an independent and unofficial API. Use at your own risk.
This project violates Linkedin's User Agreement Section 8.2, and because of this, Linkedin may (and will) temporarily or permanently ban your account. We are not responsible for your account being banned.

Input

Import libraries

from naas_drivers import linkedin
import naas
import os

Setup Variables

Mandatory
  • li_at: Cookie used to authenticate Members and API clients
  • JSESSIONID: Cookie used for Cross Site Request Forgery (CSRF) protection and URL signature validation
Optional
  • limit: This refers to the quantity of connections you wish to retrieve. If you intend to fetch all connections, use -1. Please note that retrieving all connections might be time-consuming.
  • output_dir: This variable represents the name of the output directory.
  • csv_file_name: This variable stores the name of the CSV file that will contain the latest posts.
# Mandatory
li_at = naas.secret.get("LINKEDIN_LI_AT") or "YOUR_LINKEDIN_LI_AT" #example: AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2
JSESSIONID = naas.secret.get("LINKEDIN_JSESSIONID") or "YOUR_LINKEDIN_JSESSIONID" #example: ajax:8379907400220387585
# Optional
limit = 100
output_dir = "linkedin_outputs/connections/"
csv_file_name = "connections_data.csv"

Model

Define output paths

Create the output directory and define paths for the output files.
# Check if directory exists and create it if not
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Generate outputs files path
csv_file_path = os.path.join(output_dir, csv_file_name)
print('📂 CSV file path:', csv_file_path)

Get connections from LinkedIn network

Available columns:
  • 'FIRSTNAME': This represents the first name of the connection.
  • 'LASTNAME': This is the last name of the connection.
  • 'OCCUPATION': This is the text that appears below the name on the profile page.
  • 'CREATED_AT': This is the date when the connection was established.
  • 'PROFILE_URL': This is the URL of the LinkedIn profile.
  • 'PROFILE_PICTURE': This is the URL of the profile picture.
  • 'PROFILE_ID': This is the unique LinkedIn profile ID.
  • 'PUBLIC_ID': This is the LinkedIn public profile ID."
df = linkedin.connect(li_at, JSESSIONID).network.get_connections(limit=limit)
print("✅ Row fetched:", len(df))
df.head(5)

Output

Save data to CSV

df.to_csv(csv_file_path, index=False)
print("💾 Dataframe successfully saved:", csv_file_path)

Create asset

naas.asset.add(csv_file_path)
# -> to remove your outputs, uncomment the lines and execute the cell
# naas.asset.delete(csv_output)