-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.js
180 lines (177 loc) · 7.77 KB
/
crawl.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const puppeteer = require("puppeteer");
async function crawlData(key) {
let resultObj = {};
let returnedResponse;
let browser;
try {
browser = await puppeteer.launch({
headless: false,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-infobars",
"--disable-features=site-per-process",
"--window-position=0,0",
"--disable-extensions",
'--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36"',
],
executablePath: "node_modules/puppeteer/.local-chromium/win64-782078/chrome-win/chrome.exe"
});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 800 });
if (key) {
await page.goto(
`https://www.amazon.in/s?k=${key}&tag=amdot-21&ref=nb_sb_noss`,
{ waitUntil: "load", timeout: 30000 }
);
await page.waitForSelector("#search > div.s-desktop-width-max");
returnedResponse = await page.evaluate(`(() => {
let elementArray = [];
let dataArray = [];
if (
document.querySelectorAll(
"#search > div.s-desktop-width-max.s-desktop-content.sg-row > div.sg-col-20-of-24.sg-col-28-of-32.sg-col-16-of-20.sg-col.sg-col-32-of-36.sg-col-8-of-12.sg-col-12-of-16.sg-col-24-of-28 > div > span:nth-child(4) > div.s-main-slot.s-result-list.s-search-results.sg-row > div"
).length > 0
) {
let xyz = document.querySelectorAll(
"#search > div.s-desktop-width-max.s-desktop-content.sg-row> div.sg-col-20-of-24.sg-col-28-of-32.sg-col-16-of-20.sg-col.sg-col-32-of-36.sg-col-8-of-12.sg-col-12-of-16.sg-col-24-of-28> div > span:nth-child(4) > div.s-main-slot.s-result-list.s-search-results.sg-row > div"
);
for (let divI = 3; divI < xyz.length - 4; divI++) {
elementArray.push(xyz[divI]);
}
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
for (let text = 0; text < elementArray.length; text++) {
dataArray.push({
ProductName: elementArray[text].querySelector(
"div > span > div > div > h2 > a > span"
).innerText,
productURL: elementArray[text].querySelector(
"div > span > div > div > div h2 > a "
).href,
productImg: elementArray[text].querySelector(
"div > span > div > div span > a > div > img "
).src,
price: elementArray[text].querySelector(
"div > span > div > div span.a-price-whole"
)
? elementArray[text]
.querySelector(
"div > span > div > div span.a-price-whole"
)
.innerText.trim()
.replace(/\,/, "")
: "0",
strike: elementArray[text].querySelector(
"div > span > div > div span.a-price.a-text-price .a-offscreen"
)
? elementArray[text]
.querySelector(
"div > span > div > div span.a-price.a-text-price .a-offscreen"
)
.innerText.trim()
.substr(1, 9)
.replace(/\,/, "")
: "0",
rating: elementArray[text].querySelector(
"div > span > div > div a > i "
)
? elementArray[text].querySelector(
"div > span > div > div a > i"
).innerText
: "",
offer: elementArray[text].querySelector(
"div > span > div > div > div.a-section.a-spacing-micro.s-grid-status-badge-container > a .a-badge .a-badge-text"
)
? elementArray[text].querySelector(
"div > span > div > div > div.a-section.a-spacing-micro.s-grid-status-badge-container > a .a-badge .a-badge-text"
).innerText
: "",
});
resolve(dataArray);
}
}, 4000);
});
return promise;
} else if (
document.querySelectorAll(
"#search > div.s-desktop-width-max.s-opposite-dir > div > div.sg-col-20-of-24.s-matching-dir.sg-col-28-of-32.sg-col-16-of-20.sg-col.sg-col-32-of-36.sg-col-8-of-12.sg-col-12-of-16.sg-col-24-of-28 > div > span:nth-child(4) > div.s-main-slot.s-result-list.s-search-results.sg-row > div"
).length > 0
) {
let xyz = document.querySelectorAll(
"#search > div.s-desktop-width-max.s-opposite-dir > div > div.sg-col-20-of-24.s-matching-dir.sg-col-28-of-32.sg-col-16-of-20.sg-col.sg-col-32-of-36.sg-col-8-of-12.sg-col-12-of-16.sg-col-24-of-28 > div > span:nth-child(4) > div.s-main-slot.s-result-list.s-search-results.sg-row > div"
);
console.log('xyz :',xyz)
for (let divI = 2; divI < xyz.length - 4; divI++) {
elementArray.push(xyz[divI]);
}
console.log('elementArr :',elementArray)
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
for (let text = 0; text < elementArray.length; text++) {
dataArray.push({
ProductName: elementArray[text].querySelector(
"div > span > div > div > div h2 > a > span"
).innerText,
productURL: elementArray[text].querySelector(
"div > span > div > div > div h2 > a "
).href,
productImg: elementArray[text].querySelector(
"div > span > div > div span > a > div > img "
).src,
price: elementArray[text].querySelector(
"div > span > div > div span.a-price-whole"
)
? elementArray[text]
.querySelector(
"div > span > div > div span.a-price-whole"
)
.innerText.trim()
.replace(/\,/, "")
: "0",
strike: elementArray[text].querySelector(
"div > span > div > div span.a-price.a-text-price .a-offscreen"
)
? elementArray[text]
.querySelector(
"div > span > div > div span.a-price.a-text-price .a-offscreen"
)
.innerText.trim()
.replace(/\,/, "")
.substr(1, 9)
: "0",
rating: elementArray[text].querySelector(
"div > span > div > div a > i "
)
? elementArray[text].querySelector(
"div > span > div > div a > i"
).innerText
: "",
offer: elementArray[text].querySelector(
"div.a-section div.a-section span"
)
? elementArray[text].querySelector(
"div.a-section div.a-section span"
).innerText
: "",
});
resolve(dataArray);
}
}, 4000);
});
return promise;
}
})`);
resultObj.product = returnedResponse;
}
return returnedResponse
await browser.close();
} catch (e) {
console.log("Amazon scrap error-> ", e);
await browser.close();
}
return returnedResponse;
}
module.exports = {
crawlData,
};