-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
69 lines (53 loc) · 1.59 KB
/
express.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
import Express from "express";
import puppeteer from "puppeteer";
var app=Express();
const port =8005;
app.use(Express.json());
app.post('/', async (req, res)=>{
const data=await req.body;
console.log(data);
const p=await start(data.search);
res.json(p);
} )
app.get('/', async(req, res)=>{
res.send("working scrape master")
})
app.listen(port, ()=>{
console.log("working on port " + port);
})
async function start(search) {
const browser = await puppeteer.launch({ args: [
"--disable-setuid-sandbox",
"--no-sandbox",
"--single-process",
"--no-zygote",
],
// executablePath: puppeteer.executablePath()
});
console.log(puppeteer.executablePath());
const page = await browser.newPage();
await page.goto("https://www.amazon.in");
await page.type("#twotabsearchtextbox", `${search}`);
await page.click(".nav-search-submit");
await page.waitForNavigation();
await page.waitForSelector(".s-result-item");
const images = await page.$$eval(".s-image", (texts) => {
return texts.map((x) => x.src);
});
const titles = await page.$$eval("h2.s-line-clamp-2", (texts) => {
return texts.map((x) => x.textContent);
});
const prices = await page.$$eval("span.a-price > span.a-offscreen", (texts) => {
return texts.map((x) => x.textContent);
});
let product = [];
let i = 0;
while (i < images.length && i < prices.length && i < titles.length) {
product = [...product,{ title: titles[i], image: images[i], price: prices[i] }];
//
i++;
}
console.log(product);
await browser.close();
return product;
}