-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (53 loc) · 1.91 KB
/
app.js
File metadata and controls
64 lines (53 loc) · 1.91 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
const puppeteer = require("puppeteer");
const fs = require('fs');
// anounymous function
// we have to wait for this function to get data from the website. thats why this function async
(async () => {
const browser = await puppeteer.launch({ headless: true });
// if you want to open browser insert these code line inside paranthesis `{headless: false}` example - launch({headless: false})
const page = await browser.newPage()
// below using go to function in puppeteer
await page.goto("https://www.helakuru.lk/esana/news/");
// getting screenshot for test
await page.screenshot({ path: "screenshot.png" });
// clicking notification enable button
const cancel = await page.$('.close');
await cancel.click();
await new Promise(function (resolve) {
setTimeout(resolve, 1000)
});
// create new function that can evaluate and grab data from web page
const grab = await page.evaluate(() => {
if (document.querySelector("#news_item_container > div > div > div.col-8 > div") != null) {
const news = document.querySelector("#news_item_container > div > div > div.col-8 > div");
return news.innerHTML;
}
});
// second function
const news = await page.evaluate(() => {
const results = Array.from(
document.querySelectorAll('#news_item_container > div > div > div.col-8 > div > p'),
p => p.innerText,
);
const thumb = Array.from(
document.querySelectorAll('#news_item_container > div > div > div.col-4.pr-0 > div > div > img'),
img => img.src,
);
return [{results, thumb}];
});
//console.log(news);
// save news data into json file
for (let i = 0; i < news.length; i++) {
x = news[i]
console.log(x);
x = news[i]
fs.writeFile('news.json', JSON.stringify(news[i]), err => {
if (err) {
console.error(err);
}
// file written successfully
});
}
//console.log(news[0][0]);
await browser.close();
})();