Created
January 5, 2023 03:30
-
-
Save Deamoner/bb2fb62248d955b11c1c64113a4b9090 to your computer and use it in GitHub Desktop.
nodejs function to get all the social meta data of a url
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
const request = require('request'); | |
const cheerio = require('cheerio'); | |
function getSocialMetaData(url) { | |
request(url, (error, response, html) => { | |
if (!error && response.statusCode === 200) { | |
const $ = cheerio.load(html); | |
const title = $('title').text(); | |
const description = $('meta[name="description"]').attr('content'); | |
const image = $('meta[property="og:image"]').attr('content'); | |
console.log(`Title: ${title}`); | |
console.log(`Description: ${description}`); | |
console.log(`Image: ${image}`); | |
} else { | |
console.log(`Error retrieving data: ${error}`); | |
} | |
}); | |
} | |
getSocialMetaData('https://www.example.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment