Last active
June 11, 2021 16:48
-
-
Save pbrandiezs/365093b51e1c367da2e9d49374d563ec to your computer and use it in GitHub Desktop.
This program will count the number of href's for a list of websites specified on the command line. Example: node parseHTML.js https://wikipedia.com https://perry-brandiezs.com
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
// This program will count the number of href's for a list of websites | |
// specified on the command line. | |
// | |
// Example: | |
// node parseHTML.js https://wikipedia.com https://perry-brandiezs.com | |
// https://wikipedia.com 318 | |
// https://perry-brandiezs.com 6 | |
const request = require('request'); | |
process.argv.slice(2).forEach(function (val, index, array) { | |
// console.log(index + ': ' + val); | |
request(val, function (error, response, body) { | |
// console.error('error:', error); | |
// console.log('statusCode:', response && response.statusCode); | |
// console.log('body:', body); | |
if (error) { | |
console.error('error:', error); | |
} else if (response && (response.statusCode !== 200)) { | |
console.log('statusCode:', response && response.statusCode); | |
} else { | |
// Search for href globally | |
let hrefcount = body.match(/href/g).length; | |
console.log('%s %i', val, hrefcount); | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment