-
Notifications
You must be signed in to change notification settings - Fork 2
/
headline--bbc.js
47 lines (42 loc) · 1.38 KB
/
headline--bbc.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const cheerio = require('cheerio');
const url = 'https://www.bbc.com/news';
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
// Define the user agent
page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36');
return page.goto(url, { timeout: 120000 }).then(function() {
// Wait for the dynamic content to load
return page.waitForSelector('article section a h2', 'article section a', { timeout: 60000 });
})
.then(function() {
return page.content();
});
})
.then(function(html) {
const $ = cheerio.load(html);
const articleTitle = $('article section a h2');
const articleUrl = $('article section 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: "BBC News",
}));
fs.writeFile('headlines-world/headline--bbc.json', jsonString, function(err){
console.log('File successfully written');
process.exit(0);
});
console.log(jsonString);
})
.catch(function(err) {
console.log(err);
process.exit(1);
});