-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
153 lines (138 loc) · 4.03 KB
/
app.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
require("dotenv").config();
const cheerio = require("cheerio");
const puppeteer = require("puppeteer");
const express = require("express");
const app = express();
const cors = require("cors");
const PORT = process.env.PORT || 8800;
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
const obj = {};
const scrapeProduct = async (url) => {
let browser = null;
try {
browser = await puppeteer.connect({
browserWSEndpoint:
`wss://chrome.browserless.io?token=` + process.env.API_KEY,
headless: true,
args: ["--no-sandbox"],
});
browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
let html = await page.evaluate(() => document.body.innerHTML);
const $ = await cheerio.load(html);
await $("#productTitle", html).each(function () {
let title = $(this).text();
obj.title = title.trim();
});
await $(".attach-base-product-price", html).each(function () {
let price = $(this).val();
obj.price = price;
});
//description point 1
const [el5] = await page.$x('//*[@id="feature-bullets"]/ul/li[2]');
if (!el5) {
description1 = "";
} else {
const txt4 = await el5.getProperty("textContent");
description1 = await txt4.jsonValue();
}
//description point 2
const [el6] = await page.$x('//*[@id="feature-bullets"]/ul/li[3]/span');
if (!el6) {
description2 = "";
} else {
const txt5 = await el6.getProperty("textContent");
description2 = await txt5.jsonValue();
}
obj.description = description1 + " " + description2;
const [el] = await page.$x(`//*[@id="landingImage"]`);
const src = await el.getProperty("src");
obj.image = await src.jsonValue();
return obj;
} catch (err) {
console.log("error " + err);
} finally {
if (browser) {
browser.close();
}
}
/* //new page
const page = await browser.newPage();
await page.goto(url);
//title
const getTitle = await page.$$eval("h1 span.a-size-large", (nodes) =>
nodes.map((n) => n.innerText)
);
const title = getTitle[0];
// //image
const [el] = await page.$x(`//*[@id="landingImage"]`);
const src = await el.getProperty("src");
const image = await src.jsonValue();
//price $$
const [el3] = await page.$x(
'//*[@id="corePrice_desktop"]/div/table/tbody/tr[2]/td[2]/span[1]/span[1]'
);
if (!el3) {
price = 0;
} else {
const txt2 = await el3.getProperty("textContent");
const price = awaittxt2.jsonValue();
}
//orig $$
const [el4] = await page.$x(
'//*[@id="corePrice_desktop"]/div/table/tbody/tr[1]/td[2]/span[1]/span[1]'
);
if (!el4) {
orig = 0;
} else {
const txt3 = await el4.getProperty("textContent");
const orig = await txt3.jsonValue();
}
//description point 1
const [el5] = await page.$x('//*[@id="feature-bullets"]/ul/li[2]');
if (!el5) {
description1 = "";
} else {
const txt4 = await el5.getProperty("textContent");
description1 = await txt4.jsonValue();
}
//description point 2
const [el6] = await page.$x('//*[@id="feature-bullets"]/ul/li[3]/span');
if (!el6) {
description2 = "";
} else {
const txt5 = await el6.getProperty("textContent");
description2 = await txt5.jsonValue();
}
const description = description1 + " " + description2;
browser.close();
return { title, image, description, orig, price }; */
};
/* app.get("/new/*", async (req, res) => {
try {
const url = req.params[0];
const data = await scrapeProduct(url);
res.json(data);
} catch (err) {
res.send(console.log("error " + err));
}
}); */
app.post("/scrape/", async (req, res) => {
try {
console.log(req.body);
const data = await scrapeProduct(req.body.url);
res.json(data);
} catch (err) {
res.send(console.log("error " + err));
}
});
app.get("/", async (req, res) => {
res.send("Jupiter Scrapper. Please use /new/url");
});
app.listen(process.env.PORT || PORT, () => {
console.log("running on " + PORT);
});