-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·41 lines (31 loc) · 1.12 KB
/
index.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
#!/usr/bin/env node
import puppeteer from 'puppeteer'
const scrape = async url => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(url)
const elements = await page.evaluate(() => {
const query = (selector, root = document) =>
Array.from(root.querySelectorAll(selector))
const scrapePalette = palette =>
query('.place span', palette).map(colorElement => colorElement.innerText)
const isNotBlank = string => string.trim().length > 0
const doesNotHaveBlankElements = elements => elements.every(isNotBlank)
return query('.palette')
.map(scrapePalette)
.filter(doesNotHaveBlankElements)
})
await browser.close()
return elements
}
const main = async () => {
const palettes = {
new: await scrape('https://colorhunt.co/palettes'),
trendy: await scrape('https://colorhunt.co/palettes/trendy'),
popular: await scrape('https://colorhunt.co/palettes/popular'),
random: await scrape('https://colorhunt.co/palettes/random')
}
const prettyJson = JSON.stringify(palettes, undefined, 2)
console.log(prettyJson)
}
main()