-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
128 lines (111 loc) · 4.18 KB
/
scraper.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
const puppeteer = require('puppeteer');
require("dotenv").config();
// const mongoose = require('mongoose');
// mongoose.connect('mongodb://127.0.0.1:27017/products')
// .then(() => {
// console.log("connection open");
// })
// .catch(err => {
// console.log("error");
// })
// const flipkartProduct = require('./schema/flipkart');
// const amazonProduct = require('./schema/amazon');
exports.Amazon = async function (input) {
let elements=[];
const browser = await puppeteer.launch(
{
args: [
'--no-sandbox',
'--disable-gpu',
],
headless: "new",
executablePath: process.env.NODE_ENV === "production"
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
}
);
try {
const page = await browser.newPage();
await page.goto(`https://www.amazon.in/s?k=${input}`)
const limit = 10;
elements = await page.$$eval('.a-section .puisg-row', (sections, limit) => {
const extractedData = [];
for (const section of sections.slice(0, limit)) {
try {
const innerElements = section.querySelectorAll('.a-size-medium');
const imgs = section.querySelector('.s-image').src;
const prices = section.querySelector('.a-price .a-offscreen').textContent;
const rating = section.querySelector('.a-section.a-spacing-none.a-spacing-top-micro').textContent;
const link = section.querySelector('.a-link-normal').href;
const sectionData = [];
for (const innerElement of innerElements) {
sectionData.push(innerElement.textContent);
}
const obj = {};
obj.url = imgs;
obj.title = sectionData;
obj.price = prices;
obj.rating = rating;
obj.link = link
extractedData.push(obj);
} catch (e) {
console.log("error")
}
}
return extractedData;
}, limit);
// amazonProduct.insertMany(elements);
} catch (e) {
console.log(e);
}
await browser.close();
return elements;
}
exports.Flipkart = async function (input) {
let elements=[];
const browser = await puppeteer.launch(
{
args: [
'--no-sandbox',
'--disable-gpu',
],
headless: "new",
executablePath:
process.env.NODE_ENV === "production"
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
}
);
try {
const page = await browser.newPage();
await page.goto(`https://www.flipkart.com/search?q=${input}`);
const limit = 5;
elements = await page.$$eval('._1AtVbE ._13oc-S', (sections, limit) => {
const extractedData = [];
for (const section of sections.slice(0, limit)) {
try {
const innerElements = section.querySelector('._4rR01T').textContent;
const imgs = section.querySelector('._396cs4').src;
const prices = section.querySelector('._1_WHN1').textContent;
const rating = section.querySelector('._1lRcqv').textContent;
const link = section.querySelector('._1fQZEK').href;
const obj = {};
obj.url = imgs;
obj.title = innerElements;
obj.price = prices;
obj.rating = rating;
obj.link = link
extractedData.push(obj);
} catch (e) {
console.log("error", (e));
}
}
return extractedData;
}, limit);
// flipkartProduct.insertMany(elements);
} catch (e) {
console.log(e);
}
await browser.close();
return elements;
}