How to Scrape LinkedIn Profiles with Ruby

Extract profile data from LinkedIn

💎 Using Ruby

Overview

Learn how to scrape LinkedIn profiles using Ruby. This comprehensive guide will walk you through the entire process, from setup to implementation.

What You'll Learn

  • • Setting up your development environment
  • • Installing the required HTTP client
  • • Authenticating with the ScrapeCreators API
  • • Making requests to LinkedIn
  • • Handling responses and errors
  • • Best practices for production use

What You'll Get

  • • Access to profiles data
  • • JSON formatted responses
  • • Real-time data access
  • • Scalable solution
  • • Error handling patterns
  • • Performance optimization tips

Prerequisites

1. API Key

First, you'll need a ScrapeCreators API key to authenticate your requests.

Sign up at app.scrapecreators.com to get your free API key with 100 requests.

2. Development Environment

Make sure you have the following installed:

  • Ruby and its dependencies
  • • A code editor (VS Code, Sublime, etc.)
  • • Basic understanding of API requests
  • • Command line interface access

Step 1: Install HTTP Client

HTTParty is a Ruby library that makes HTTP requests easy

gem
gem install httparty

Step 2: API Implementation

Now let's make a request to the LinkedIn API using Ruby. Replace YOUR_API_KEY with your actual API key.

Ruby
require 'httparty'
require 'json'

API_KEY = 'YOUR_API_KEY'

def scrape
  response = HTTParty.get(
    "https://api.scrapecreators.com/v1/linkedin/profile",
    headers: {
      'x-api-key' => API_KEY,
      'Content-Type' => 'application/json'
    },
    query: {
        'url' => 'https://www.linkedin.com/in/parrsam/'
    }
  )

  puts 'Response:'
  puts JSON.pretty_generate(response.parsed_response)
  response.parsed_response
rescue => e
  puts "Error: #{e.message}"
  nil
end

# Usage
result = scrape

Step 3: Testing Your Code

API Parameters

This endpoint accepts the following parameters:

urlRequired(string)

The URL of the LinkedIn profile to get

Example: https://www.linkedin.com/in/parrsam/

Run Your Code

Execute your script to test the API connection. You should see a JSON response with LinkedIn profiles data.

✅ Success: You should receive a structured JSON response containing the requested data.

Expected Response

Here's an example of the JSON response you'll receive:

Sample Response
{
  "success": true,
  "name": "Sam Parr",
  "image": "https://media.licdn.com/dms/image/v2/C4E03AQH3Vz1qV_rNVQ/profile-displayphoto-shrink_200_200/profile-displayphoto-shrink_200_200/0/1633389534107?e=2147483647&v=beta&t=8x4PIfTIYFOvpI1QRCDyAhNprZv0NY9kAp4EOdvUxLQ",
  "location": "Westport, Connecticut, United States",
  "followers": 64803,
  "connections": "",
  "about": "I founded The Hustle, a business news media company with $12 when I was around 25 years…",
  "recentPosts": [
    {
      "title": "Super excited to watch the success of a company built by a Gauntlet AI grad: One missed contract renewal can cost your company $100k, $250k or more.…",
      "activityType": "Posted by Austen Allred",
      "link": "https://www.linkedin.com/posts/austenallred_super-excited-to-watch-the-success-of-a-company-activity-7333305862695919617-zTPI",
      "image": "https://static.licdn.com/aero-v1/sc/h/53n89ecoxpr1qrki1do3alazb"
    },
    {
      "title": "Imagine if software engineers declared that programming doesn't work if their program didn't compile on the first try. That's basically the level of…",
      "activityType": "Posted by Austen Allred",
      "link": "https://www.linkedin.com/posts/austenallred_imagine-if-software-engineers-declared-that-activity-7325898973485879296-jTB4",
      "image": "https://static.licdn.com/aero-v1/sc/h/53n89ecoxpr1qrki1do3alazb"
    }
  ],
  "experience": [
    {
      "@type": "Organization",
      "name": "Hampton",
      "url": "https://www.linkedin.com/company/myhampton",
      "location": "Austin, Texas Metropolitan Area",
      "member": {
        "@type": "OrganizationRole",
        "description": "******* ** * ****** ****** ********** ********* *** *************, ******** *** ****. <**><**>**'** ** * *** ****, *** ***...* *****.<**><**>*****://***.***********.***/"
      }
    },
    {
      "@type": "Organization",
      "name": "** ***** *******",
      "member": {
        "@type": "OrganizationRole",
        "description": "** ***** ******* ** * ******* ***** ** *** ******. ***** **** *** * *** *** *****.<**><**>**** ***** **** ** **** *** ****** ** **** ****.<**><**>**'** *** *** ** *** **** ******** ** ******* ******** **** **** ** ******** ** *********."
      }
    }
  ],
  "articles": [
    {
      "headline": "RANT: How to PROPERLY analyze risk",
      "author": "Sam Parr",
      "datePublished": "2017-09-17T13:08:55.000+00:00",
      "image": "https://media.licdn.com/dms/image/v2/C5612AQG9YbLJodxr8g/article-cover_image-shrink_720_1280/article-cover_image-shrink_720_1280/0/1520096757452?e=2147483647&v=beta&t=USPma8tkR65yot8_Ye0zDfH2llLpTrZdZGHtbzmPjQ0",
      "articleBody": "I've been talking to a few people who want to leave...."
    }
  ],
  "activity": [
    {
      "title": "Othership is this cool suana place in NYC. The founder is hosting a bunch of Hampton members for a special session + a talk on how he built the…",
      "activityType": "Shared by Sam Parr",
      "link": "https://www.linkedin.com/posts/parrsam_exclusive-othership-session-qa-with-founder-activity-7328134812047704064-4NUL",
      "image": "https://media.licdn.com/dms/image/sync/v2/D4D27AQGJaeiyu-gbrA/articleshare-shrink_1280_800/B4DZbLDrHUHsAU-/0/1747163460743?e=2147483647&v=beta&t=rZtGF2kjhFfLy-TZil0d_bakH4UfHnK-dcxEsrZSl5s"
    },
    {
      "title": "If you live in SF, LA ...and want to lead a bunch of startup ceos/founders ...and want to have the best network ever then this is for you: I'm…",
      "activityType": "Shared by Sam Parr",
      "link": "https://www.linkedin.com/posts/parrsam_careers-activity-7328088180820193281-3j6g",
      "image": "https://media.licdn.com/dms/image/sync/v2/D4E27AQFwp04bg9zFpg/articleshare-shrink_800/articleshare-shrink_800/0/1743914613899?e=2147483647&v=beta&t=PWPWBgbYTxLt6u53gLNzEO0laXD2AFd5LBPKId4Fm9Y"
    }
  ],
  "education": [
    {
      "@type": "EducationalOrganization",
      "name": "Belmont University",
      "url": "https://www.linkedin.com/school/belmont-university/",
      "member": {
        "@type": "OrganizationRole",
        "startDate": 2008,
        "endDate": 2012
      }
    }
  ],
  "publications": [
    {
      "name": "How my partner and I created a unique company in tough times",
      "url": "http://online.wsj.com/article/SB10001424127887324694904578602013087282582.html&urlhash=SvYD"
    },
    {
      "name": "Our infographic and app get a shout out in the Huffington Post",
      "url": "http://www.huffingtonpost.com/2013/08/09/roommates-app-la_n_3733587.html&urlhash=u66P"
    },
    {
      "name": "Shout from the LA Times",
      "url": "http://www.latimes.com/business/technology/la-fi-tn-roommate-finding-apps-20130816,0,595590.story&urlhash=yblh"
    }
  ],
  "projects": [
    {
      "name": "Hustle Con 2015",
      "url": "https://www.linkedin.com/in/elizabethyin",
      "dateRange": "2015",
      "description": "",
      "contributors": [
        {
          "name": "Elizabeth Yin",
          "link": "https://www.linkedin.com/in/elizabethyin",
          "image": "https://media.licdn.com/dms/image/v2/C5603AQFFdhmfyyiZig/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1581292451105?e=2147483647&v=beta&t=IwghMTQZZjJzIGbN6e1IYfWVBDAkgLP_kSJJPOkzQDg"
        }
      ]
    },
    {
      "name": "The Roommates App",
      "url": "http://roommate.apartmentlist.com/",
      "dateRange": "Mar 2013 - Present",
      "description": "Roommates is an easy and fun way to meet new potential roommates. It functions much like Tinder, in that you browse profiles one at a time and are only able to chat once you have mutual interest in each other.",
      "contributors": [
        {
          "name": "Jake Moffatt",
          "link": "https://www.linkedin.com/in/jakeonrails",
          "image": "https://media.licdn.com/dms/image/v2/D5603AQFi9y-GqH832w/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1719008688472?e=2147483647&v=beta&t=lelf--Z_vVdOKUfXM_LTKRgUgUTkpmdKS4fyOFJeYLc"
        },
        {
          "name": "John Havel",
          "link": "https://www.linkedin.com/in/jhavel",
          "image": "https://media.licdn.com/dms/image/v2/C5603AQEKBX3RdWYlSw/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1588215318616?e=2147483647&v=beta&t=WiHWmfUczafX3mAYnS8ZBLGKFXISlV_FZkHW5oqyUJM"
        }
      ]
    }
  ],
  "recommendations": [
    {
      "name": "Jackie M.",
      "link": "https://www.linkedin.com/in/jackiemooney",
      "image": "https://static.licdn.com/aero-v1/sc/h/9c8pery4andzj6ohjkjp54ma2",
      "text": "“I got to know Sam over 2 years of working at The Hustle. We grew from a company of 4 to 15 in that time and there was never a dull moment- anyone who knows Sam knows that he adds tons of energy to the workplace. This guy figures out what he wants to accomplish and makes it happen. He remains wildly driven and goal-oriented while constantly focused on both his personal and professional growth.”"
    },
    {
      "name": "Michael Cheung",
      "link": "https://www.linkedin.com/in/themichaelcheung",
      "image": "https://media.licdn.com/dms/image/v2/C5603AQGBj0wY-4ePhg/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1586460717915?e=2147483647&v=beta&t=uGi9GocopqdNH3aUPCv5hEvmnq24YR8VBbzBAz_Pmbw",
      "text": "“Sam is a hustler that and serial entrepreneur. He just figures out how to get things done and \"win\". Mark my words, this kid is going places!”"
    }
  ],
  "similarProfiles": [
    {
      "link": "https://ca.linkedin.com/in/stevemcody",
      "name": "Steve Cody",
      "image": "https://media.licdn.com/dms/image/v2/D5603AQEl17hna0u1ew/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1673387873184?e=2147483647&v=beta&t=pN-Y2CS1AQ8AOKbTo_2_B0AXjof4nGLKBdhNZTfFYJA"
    }
  ]
}

Verify Response Structure

Check that your response includes the expected fields:

  • success(boolean)
  • name(string)
  • image(string)
  • location(string)
  • followers(number)
  • ... and 11 more fields

Best Practices

1

Error Handling

Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.

2

Caching

Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.

3

Security

Never expose your API key in client-side code. Use environment variables and secure key management practices.

Performance Tips

Batch Requests

When scraping multiple profiles, consider batching requests to maximize throughput while staying within rate limits.

Async Processing

Use asynchronous processing in Ruby to handle multiple requests concurrently and improve overall performance.

Common Use Cases

Market Research

Analyze LinkedIn profiles to understand market trends, competitor analysis, and audience insights.

Content Analytics

Track performance metrics, engagement rates, and content trends across LinkedIn profiles.

Lead Generation

Identify potential customers and business opportunities throughLinkedIn data analysis.

Troubleshooting

Common Errors

401 Unauthorized

Check your API key is correct and properly formatted in the x-api-key header.

402 Payment Required

You ran out of credits and need to buy more.

404 Not Found

The resource might not exist or be private.

500 Server Error

Temporary server issue. Implement retry logic with exponential backoff.

Frequently Asked Questions

How much does it cost to scrape LinkedIn profiles?

ScrapeCreators offers 100 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.

Is it legal to scrape LinkedIn data?

Scraping publicly available data is fair game, and we only collect public data. So anything that you can see in an incognito browser is what we collect.

How fast can I scrape LinkedIn profiles?

There is no rate limit! So you can scrape as fast as you want!

What data format does the API return?

All API responses are returned in JSON format, making it easy to integrate with any programming language or application.

Can I use this with other Ruby frameworks?

Yes! This tutorial focuses on core Ruby HTTP concepts that work with any framework. The API calls remain the same regardless of your specific Ruby setup.

How do I handle large datasets?

For large datasets, implement pagination, use streaming responses where available, and consider storing data in a database for efficient querying.

Related Tutorials

Ready to Start Scraping?

Get started with 100 free API calls. No credit card required.