Skip to content

Instantly share code, notes, and snippets.

@sakshamsaxena
Last active October 19, 2018 08:07
Show Gist options
  • Save sakshamsaxena/d9eaf50d0067ff02f7f7c75b76b42d0a to your computer and use it in GitHub Desktop.
Save sakshamsaxena/d9eaf50d0067ff02f7f7c75b76b42d0a to your computer and use it in GitHub Desktop.
A simple JavaScript Snippet to get the list of all the links with titles on it's "Must Do Coding Questions" page.
// Extracted from "https://www.geeksforgeeks.org/must-do-coding-questions-for-companies-like-amazon-microsoft-adobe/" on April 7, 2018
/* How To Use :
1. Install Node.js
2. Fire up your console and create a fresh directory and enter it.
3. Run `npm init -y && npm i superagent cheerio`
4. Save this file in that directory.
5. Run it with `node GFGExtractor.js`
*/
/*
Required Modules
*/
const request = require('superagent')
const cheerio = require('cheerio')
/*
Global Variables
*/
var url, $
/*
Main Application
*/
url = 'https://www.geeksforgeeks.org/must-do-coding-questions-for-companies-like-amazon-microsoft-adobe/'
request
.get(url)
.end(function (err, res) {
// The HTML is returned with Content-Type "text/plain", hence it is added to the res.text key
$ = cheerio.load(res.text)
// $ now is exact equivalent to the jQuery object which consists of the entire DOM Tree.
// Extract the meat!
var titles = {}
$("p[align='center']").each(function(){
var title = $(this).text().trim().replace(" :", "")
var list = $(this).next("ol")
var urls = []
$(list).find("li").each(function() {
var o = {}
o.name = $(this).text().trim()
o.url = $(this).find("a").attr("href").trim()
urls.push(o)
})
titles[title] = urls
})
// Log the meat for now
console.log(titles)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment