Last active
September 25, 2024 07:45
-
-
Save ErickWendel/a5737b92f09cf07d777c33e22afafffb to your computer and use it in GitHub Desktop.
Example of how to get post Analytics such as Views from a Linkedin post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// paste this file on a empty directory | |
// npm i axios | |
// You should go to your browser on Cookie session and get JSESSIONID and li_at from Linkedin Section | |
const JSESSIONID = 'YOUR JSESSIONID' | |
const liAT = 'YOUR li_at' | |
import axios from 'axios' | |
const headers = { | |
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36", | |
Cookie: `JSESSIONID=${JSESSIONID}; li_at=${liAT};`, | |
"csrf-token": `${JSESSIONID}` | |
}; | |
function getPostId(link) { | |
const postId = link.match(/activity-(?<postId>\d+)/)?.groups?.postId | |
return postId | |
} | |
async function getLinkedinViews(link) { | |
const postId = getPostId(link) | |
if(!postId) { | |
console.error(`postId not found!`, link) | |
return; | |
} | |
const url = `https://www.linkedin.com/voyager/api/feed/updatesV2?commentsCount=0&likesCount=0&q=backendUrnOrNss&urnOrNss=urn:li:activity:${postId}` | |
const { data: file } = await axios.get(url, { | |
headers | |
}) | |
// just to save time, I got the property using regex :) | |
const views = JSON.stringify(file).match(/"numViews":(?<views>\d+)/)?.groups?.views | |
return views | |
} | |
const postURL = 'https://www.linkedin.com/posts/erickwendel_dica-de-ouro-para-entrevistas-em-ingl%C3%AAs-activity-6764509683727265792-l7v1' | |
const result = await getLinkedinViews(postURL) | |
console.log(result) | |
// 21608 | |
// this example was based on this article: | |
// https://towardsdatascience.com/using-browser-cookies-and-voyager-api-to-scrape-linkedin-via-python-25e4ae98d2a8 |
Cannot use import statement outside a module
look that the file must be named as .mjs
not .js
as it's an ecmascript module.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cannot use import statement outside a module