-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGoogleShoppingScraper.js
More file actions
76 lines (63 loc) · 2.3 KB
/
GoogleShoppingScraper.js
File metadata and controls
76 lines (63 loc) · 2.3 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const unirest = require("unirest");
const cheerio = require("cheerio");
const getShoppingData = () => {
try
{
return unirest
.get("https://www.google.com/search?q=nike shoes&tbm=shop&gl=us")
.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 ads = [];
$(".sh-np__click-target").each((i,el) => {
ads.push({
title: $(el).find(".sh-np__product-title").text(),
link: "https://google.com" + $(el).attr("href"),
source: $(el).find(".sh-np__seller-container").text(),
price: $(el).find(".hn9kf").text(),
delivery: $(el).find(".U6puSd").text(),
})
if($(el).find(".rz2LD").length)
{
let extensions = []
extensions = $(el).find(".rz2LD").text()
ads[i].extensions = extensions
}
})
for (let i = 0; i < ads.length; i++) {
Object.keys(ads[i]).forEach(key => ads[i][key] === "" ? delete ads[i][key] : {});
}
let shopping_results = [];
$(".sh-dgr__gr-auto").each((i,el) => {
shopping_results.push({
title: $(el).find(".Xjkr3b").text(),
link: $(el).find(".zLPF4b .eaGTj a.shntl").attr("href").substring($(el).find("a.shntl").attr("href").indexOf("=")+1),
source: $(el).find(".IuHnof").text(),
price: $(el).find(".XrAfOe .a8Pemb").text(),
rating: $(el).find(".Rsc7Yb").text(),
reviews: $(el).find(".NzUzee div").attr("aria-label") ? $(el).find(".NzUzee div").attr("aria-label").substring(0,$(el).find(".NzUzee div").attr("aria-label").indexOf(" ")) : "",
delivery: $(el).find(".vEjMR").text()
})
if($(el).find(".Ib8pOd").length)
{
let extensions = [];
extensions = $(el).find(".Ib8pOd").text();
shopping_results[i].extensions = extensions
}
})
for (let i = 0; i < shopping_results.length; i++) {
Object.keys(shopping_results[i]).forEach(key => shopping_results[i][key] === "" ? delete shopping_results[i][key] : {});
}
console.log(ads)
console.log(shopping_results)
})
}
catch(e)
{
console.log(e)
}
}
getShoppingData();