Scraping
1
min read

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

This detailed guide will explore the intricacies of utilizing the Piloterr API to retrieve the freshest posts, providing insights into the process and highlighting its potential for informed decision-making and strategic connections.
Written by
Josselin Liebe
Published on
January 19, 2024
Readers rating
28
votes

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

Count of LinkedIn posts found: 9. Fetching details...
[{'id': '7043937317610287104', 'url': 'https://www.linkedin.com/posts/najettefellache_i-strongly-believe-that-employees-know-how-activity-7043937317610287104-0thT', 'text': "I strongly believe that employees’ know-how is the company's most valuable asset.\n\nUnlike onsite workers, remote workers can't just tap on a teammate's shoulder to ask a question\n\nThey need a different way to communicate and acquire knowledge.\n\nThe modern distributed workforce works with asynchronous video: \n\n✨ They record their questions
 in a video\xa0\n✨ Their teammate record their comment in the same video.\n✨ This video is now accessible to others.\n\nImagine all employees doing this ---\n\n-- It’s a great way to capture informal knowledge and build a company video knowledge hub.", 'author'
: {'url': 'https://www.linkedin.com/in/najettefellache', 'full_name': 'Najette Fellache 🔆', 'image_url': 'https://media.licdn.com/dms/image/D4E03AQGvUg-_OXZOtg/profile-displayphoto-shrink_200_200/0/1695157263651?e=2147483647&v=beta&t=Umsm2F-YlywXU3Za83V8QtjvXFjCvoGSmFszmfTzKe4', 'profile_type': 'person'}, 'hashtags': [], 'image_url': 'https://media.licdn.com/dms/image/C5622AQGsOm8ykXqCGg/feedshare-shrink_2048_1536/0/1679405525994?e=2147483647&v=beta&t=mncAP_UKZHrUMmTByr4LeRrtrHujyGUNb3-7SM6tDco', '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.

Piloterr web scraping api
Scrape linkedin post
Discover Piloterr, the all-in-one scraping API. Sign up now and get 1000 free requests per month.⚡

Take a look at our blog posts

Interviews, tips, guides, industry best practices and news.
10 Best Practices For A Successful Data Strategy
News
7
min read

10 Best Practices For A Successful Data Strategy

Learn the essentials of data management, including the creation of guidelines, identification...
Read post
How to Get Latest Linkedin Posts or Activities with an API ? [2024]
Scraping
1
min read

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

How to find the latest linkedin posts related to a topic
Read post
5 Scraping Tools on Leboncoin in 2024 [No Code and Dev]
Scraping
2
min read

5 Scraping Tools on Leboncoin in 2024 [No Code and Dev]

Reviews the top five scraping tools suitable for Leboncoin
Read post