Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created April 24, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revolunet/d8a60950c6d5c1bd442967d284def880 to your computer and use it in GitHub Desktop.
Save revolunet/d8a60950c6d5c1bd442967d284def880 to your computer and use it in GitHub Desktop.
Google related questions
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));
@revolunet
Copy link
Author

install :

npm i node-fetch jsdom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment