-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdinheirovivoHeadline.js
49 lines (43 loc) · 1.37 KB
/
dinheirovivoHeadline.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const cheerio = require('cheerio');
const url = 'https://www.dinheirovivo.pt';
puppeteer
.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'] // Added no-sandbox flag
})
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
// Wait for the dynamic content to load
return page.waitForSelector('.arr--headline > a > .headline-m_headline__3_NhV', '.arr--headline > a');
})
.then(function() {
return page.content();
});
})
.then(function(html) {
const $ = cheerio.load(html);
const articleTitle = $('.arr--headline > a > .headline-m_headline__3_NhV');
const articleUrl = $('.arr--headline > a');
let ms = new Date();
const dateIso = ms.toISOString()
const jsonString = JSON.stringify(Object.assign({}, {
title: articleTitle.first().text().trim(),
url: articleUrl[0].attribs.href,
fetchDate: dateIso,
media: "Dinheiro Vivo"
}));
fs.writeFile('headlines/headline--dinheirovivo.json', jsonString, function(err){
console.log('File successfully written');
// Exit the process after the file is written
process.exit(0);
});
console.log(jsonString);
})
.catch(function(err) {
console.log(err);
process.exit(1);
});