Created
April 24, 2019 08:06
-
-
Save revolunet/d8a60950c6d5c1bd442967d284def880 to your computer and use it in GitHub Desktop.
Google related questions
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 fetch = require("node-fetch"); | |
const jsdom = require("jsdom"); | |
const { JSDOM } = jsdom; | |
const search = query => { | |
const url = `https://www.google.fr/search?q=${encodeURIComponent(query)}`; | |
return fetch(url, { | |
headers: { | |
"user-agent": | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" | |
} | |
}).then(r => r.text()); | |
}; | |
const extractRelated = html => { | |
const dom = new JSDOM(html); | |
const document = dom.window.document; | |
return Array.from(document.querySelectorAll(".related-question-pair")) | |
.map(node => node.querySelector(".match-mod-horizontal-padding")) | |
.map(node => node.textContent); | |
}; | |
search("kikoo lol") | |
.then(extractRelated) | |
.then(related => console.log(related)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
install :
npm i node-fetch jsdom