-
Notifications
You must be signed in to change notification settings - Fork 8
/
GoogleorganicResultsScraper.js
46 lines (40 loc) · 1.2 KB
/
GoogleorganicResultsScraper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const unirest = require("unirest");
const cheerio = require("cheerio");
const getOrganicData = () => {
return unirest
.get("https://www.google.com/search?q=javascript&gl=us&hl=en")
.headers({
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
})
.then((response) => {
let $ = cheerio.load(response.body);
let titles = [];
let links = [];
let snippets = [];
let displayedLinks = [];
$(".yuRUbf > a > h3").each((i, el) => {
titles[i] = $(el).text();
});
$(".yuRUbf > a").each((i, el) => {
links[i] = $(el).attr("href");
});
$(".g .VwiC3b ").each((i, el) => {
snippets[i] = $(el).text();
});
$(".g .yuRUbf .NJjxre .tjvcx").each((i, el) => {
displayedLinks[i] = $(el).text();
});
const organicResults = [];
for (let i = 0; i < titles.length; i++) {
organicResults[i] = {
title: titles[i],
links: links[i],
snippet: snippets[i],
displayedLink: displayedLinks[i],
};
}
console.log(organicResults)
});
};
getOrganicData();