-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes.js
52 lines (46 loc) · 1.24 KB
/
quotes.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
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
const url = 'http://quotes.toscrape.com/';
const file = path.join(__dirname, 'data/quotes.json');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(url);
let json = [];
let next = await page.$('.pager .next a');
while (next) {
next = await page.$('.pager .next a');
const quotes = await page.$$eval('.quote', getData);
json = json.concat(quotes);
if (next) {
await Promise.all([
page.waitForNavigation(),
page.click('.pager .next a'),
]);
}
}
fs.writeFileSync(file, JSON.stringify(json), 'utf8');
await browser.close();
})();
/**
*
* @param {Array} quotes
* @return {Array}
*/
function getData(quotes) {
return quotes.map((quote) => {
return {
quote: quote.querySelector('.text').textContent,
author:
quote.querySelector('.author')
? quote.querySelector('.author').textContent
: '',
tags:
quote.querySelector('.tags')
? Array.from(quote.querySelectorAll('.tags .tag'))
.map((tag) => tag.textContent)
: [],
};
});
}