How to Get Latest Linkedin Posts or Activities with an API ? [2024]

How to Get Latest Linkedin Posts or Activities with an API ? [2024]
Scraping

In the fast-paced world of professional networking, staying updated with the latest activities/posts on LinkedIn can be a crucial aspect for many businesses and individuals. As a developer, leveraging Piloterr's API to access these activities not only keeps you informed but also opens up a plethora of opportunities for data analysis, trend monitoring, and targeted networking. In this comprehensive guide, we'll dive into how to harness the Piloterr API to fetch the most recent activities.

The script we're sharing with you describes a method for retrieving LinkedIn activities matching specific keywords or hashtags, using the APIs provided by Piloterr.

How to run the code?

  1. Copy the code
  2. Create a new file get_linkedin_activities_from_google.py
  3. Replace the API token with your own
  4. Replace QUERY with your sector, a business topic or any other subject you wish to analyze
  5. Run the script with py get_linkedin_activities_from_google.py
import requests

PILOTERR_API_KEY = 'YOUR-TOKEN-API-REPLACE-ME'
QUERY = 'Formation'

def get_linkedin_post_details(known_url: str):
    api_url = "https://piloterr.com/api/v2/linkedin/post/info"
    headers = {
        "x-api-key": PILOTERR_API_KEY
    }
    params = {
        "query": known_url
    }
    response = requests.get(
        url=api_url,
        headers=headers,
        params=params
    )
    if response.status_code == 200:
        return response.json()
    else:
        return "Error: Unable to fetch data"

def get_linkedin_post_url_from_google(query: str):
    google_api_url = "https://piloterr.com/api/v2/google/search"
    headers = {
        "x-api-key": PILOTERR_API_KEY
    }
    data = {
        "query": query
    }
    response = requests.post(
        url=google_api_url,
        headers=headers,
        json=data
    )

    if response.status_code == 200:
        search_results = response.json()['organic_results']

        # Extract LinkedIn URL from search results
        for result in search_results:
            if 'linkedin.com/posts' in result['link']:
                posts.append(result['link'])
        return "LinkedIn URL not found in search results"
    else:
        return "Error: Unable to perform search"

# Example usage
posts = []
search_query = f"site:linkedin.com inurl:posts after:2023-01-01 besoin AROUND(5) {QUERY}"
linkedin_url = get_linkedin_post_url_from_google(search_query)

print(f"Count of LinkedIn posts found: {len(posts)}. Fetching details...")

parsed_posts = []

for p in posts:
    get_post_details = get_linkedin_post_details(p)
    parsed_posts.append(get_post_details)

print(parsed_posts)

Here's a description of how the script works

1. Extracting LinkedIn Posts Using Google Search

The script uses a search query with specific parameters to find LinkedIn posts via Google Search. This is achieved using the Piloterr API for Google search.

It employs search operators like site:, inurl:, after:, and AROUND() to refine the search to specific LinkedIn posts containing the desired keywords within a certain date range.

2. Collecting Post Details from LinkedIn

Once the URLs of LinkedIn posts are obtained from Google Search results, the script uses another Piloterr API : Linkedin Post Info to fetch detailed information about each post.

This information can include the content of the post, author details, number of comments, likes, etc.

3. Script Execution:

  1. The script initializes an empty list posts to store the URLs of LinkedIn posts.
  2. It then forms a search query incorporating the desired keywords and search parameters.
  3. After obtaining the URLs, the script fetches details for each post and stores them in parsed_posts.

Result

[
  {
    "id": "7043937317610287104",
    "url": "https://www.linkedin.com/posts/najettefellache_i-strongly-believe-that...",
    "text": "I strongly believe that employees’ know-how is the company's most...",
    "author": {
      "url": "https://www.linkedin.com/in/najettefellache",
      "full_name": "Najette Fellache 🔆",
      "image_url": "https://media.licdn.com/dms/image/D4E03AQGvUg...",
      "profile_type": "person"
    },
    "hashtags": [],
    "image_url": "https://media.licdn.com/dms/image/C5622AQGsOm8y...",
    "like_count": 125,
    "comments_count": 4,
    "date_published": "2023-03-21T13:32:06.530Z",
    "total_engagement": 129,
    "mentioned_profiles": []
  }
]

Conclusion

The article explains how using specific keywords to search and analyze LinkedIn activities can be a powerful tool. This method helps in tracking trends, analyzing data, and improving networking. It keeps users up-to-date in their industry and supports strategic growth in the digital world.