-
Notifications
You must be signed in to change notification settings - Fork 2
/
headline--lemonde.js
45 lines (40 loc) · 1.31 KB
/
headline--lemonde.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const cheerio = require('cheerio');
const url = 'https://www.lemonde.fr/en/';
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(url, { timeout: 90000 }).then(function() {
// Wait for the dynamic content to load
return page.waitForSelector('.zone--homepage .article--main > a > .article__title', '.zone--homepage .article--main > a', { timeout: 20000 });
})
.then(function() {
return page.content();
});
})
.then(function(html) {
const $ = cheerio.load(html);
const articleTitle = $('.zone--homepage .article--main > a > .article__title');
const articleUrl = $('.zone--homepage .article--main > 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: "Le Monde",
}));
fs.writeFile('headlines-world/headline--lemonde.json', jsonString, function(err){
console.log('File successfully written');
process.exit(0);
});
console.log(jsonString);
})
.catch(function(err) {
console.log(err);
process.exit(1);
});