Search

Crack Stats

Tag

python

Using LinkedIN API with Python to retrieve Connections’ Details

Ever faced a problem extracting your LinkedIN connections’ details one by one, don’t worry after reading this post you will able to scrape all your connections’ details in one go. To follow what is next you need to have a basic understanding of Python and an account on LinkedIN.

    • Step 1: Sign up at  https://www.linkedin.com/secure/developer and click on “Add New Application
      1
    • Step 2: Fill the compulsory fields, accept the terms & conditions and click on “Add Application
      2
    • Step 3: On the next screen you will receive some keys and ids that we will we using in next steps. These keys are specific to you and are like “passwords” to access your account, so keep them safe
      3
    • Step 4: Open a text editor, copy the code below and paste it. Update the consumer_key, consumer_secret, user_token and user_secret as you have received in step 3
#Including the libraries that needed for this program
import oauth2 as oauth
import httplib2
import time, os, simplejson, csv
import demjson

#Authentication and connecting with the server
consumer_key = 'XXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXX'
user_token = 'XXXXXXXXXXXXXXXXXXX'
user_secret = 'XXXXXXXXXXXXXXXXXXXXX'
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
access_token = oauth.Token(
key=user_token,
secret=user_secret)
client = oauth.Client(consumer, access_token)

#Retrieving connections' your connection data. Update the consumer_key, consumer_secret, user_token and user_secret#as yours
resp,content = client.request("https://api.linkedin.com/v1/people/~/connections?format=json", "GET", "")

#Cleaning the data received
data =demjson.decode(content)
v = data.values()[3][0:]
k = v[1].keys()[0:]

#Writing the data to a CSV file 
f = csv.writer(open("connection_detail.csv", "wb+"))
f.writerow(v[1].keys()[0:])
for i in range(len(v)):
    f.writerow(v[i].values()[0:])
  • Step 5: Execute the above code in Python, it will generate a CSV file in the working directory named “connection_detail.csv” that has all your connections’ data.

Installing Ipython

Before we get in to the analytics part, you need to have a few things on your system. First of all a Python distribution, aside from the official CPython distribution available from www.python.org, some of the other distributions(see a full list here) based on CPython are Anaconda by Continuum Analytics and Enthought’s EPD. I prefer using Anaconda, you can download any other distribution as well. Anaconda includes over 195 of the most popular Python packages for science, math, engineering and data analysis. You can download and install Anaconda by visiting www.continuum.io/downloads for free.

Source:- www.continuum.io/downloads
Source:- http://www.continuum.io/downloads

We also need IPython, which is a command shell for interactive computing in Python, it offers enhanced introspection, rich media, additional shell syntax, tab completion, and rich history. Follow these simple steps to install/update Ipython on Anaconda:

  1. Open Anaconda Command Prompt.
  2. Type “conda update conda” and press “Enter”.conda update
  3. After that, type “conda update ipython” and press “Enter” to update/install Ipython.update ipython

We also need Pandas Python package. Pandas provides fundamental high-level building block for doing practical, real world data analysis in Python. You can download and install Pandas by visiting  www.pypi.python.org/pypi/pandas/0.14.0/ . Select a suitable package depending upon the platform and Python version on your system.select panda

After downloading, install Pandas on your system.panda

Getting started with Python

Source:- www.python.org
Image source:- http://www.python.org

Python is one most widely used programming language, it is used for general purposes as well as high level programming. It has a very simple and consistent syntax, also with Python one can be quickly introduced to basic concepts such as conditional statements and loops. So, if you have done coding before in college or school level, it would be much easier to learn Python.

Image Source:- www.codecademy.com
Image Source:- http://www.codecademy.com

There are many programming/coding websites available who teaches Python. I found www.codecademy.com to be good enough for beginners as well as for those who know a bit of coding already. Codecademy offers an online course on Python for beginners, you can register there for free and start learning. It is a 13 hours course which covers almost all the basic concepts of Python like strings, list, dictionaries, functions, loops and conditionals statements etc. . The first few assignments are quite easy to understand. While learning you can also check your progress on Codeacademy.

Image Source:- www.codecademy.com
Image Source:- http://www.codecademy.com

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started