Last active
February 25, 2021 06:47
-
-
Save aharshac/4526be3ded1a096819017ab3fca57439 to your computer and use it in GitHub Desktop.
Node.js snippet to fetch URL title
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
var request = require('request'); | |
var cheerio = require('cheerio'); | |
function fetchTitle(url, onComplete = null) { | |
request(url, function (error, response, body) { | |
var output = url; // default to URL | |
if (!error && response.statusCode === 200) { | |
var $ = cheerio.load(body); | |
console.log(`URL = ${url}`); | |
var title = $("head > title").text().trim(); | |
console.log(`Title = ${title}`); | |
output = `[${title}](${url})`; | |
} else { | |
console.log(`Error = ${error}, code = ${response.statusCode}`); | |
} | |
console.log(`output = ${output} \n\n`); | |
if (onComplete) onComplete(output); | |
}); | |
} | |
fetchTitle("https://github.com/aharshac/KandyCpp"); | |
fetchTitle("https://www.collaborizm.com/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is using a deprecated module, Step Up your game and use an alternative of request, but maybe add it to a new file in the same gist, keep this one for archival