-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.68 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const puppeteer = require('puppeteer')
const sharp = require('sharp')
const axios = require('axios')
const path = require('path')
async function downloadAndConvertImage(url, savePath) {
try {
const response = await axios({
url,
responseType: 'arraybuffer'
})
const buffer = Buffer.from(response.data, 'binary')
await sharp(buffer).toFormat('webp').toFile(savePath)
console.log(
`Image downloaded and converted to webp successfully: ${savePath}`
)
} catch (error) {
console.error(`Error downloading or converting the image: ${error}`)
}
}
async function scrapeFirstImagesFromReactWebsite(websiteUrl) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(websiteUrl, { waitUntil: 'networkidle0' })
const imageUrls = await page.evaluate(() => {
// This is the selector for the doms that contain the images
const targetDoms = document.querySelectorAll('.container-list-item')
const urls = []
// Loop through the doms and get the image URLs
targetDoms.forEach((item) => {
const imgElement = item.querySelector('img')
if (imgElement) {
urls.push(imgElement.src)
}
})
return urls
})
if (imageUrls.length > 0) {
for (const [index, imageUrl] of imageUrls.entries()) {
const savePath = path.resolve(__dirname, `img/${index}.webp`)
await downloadAndConvertImage(imageUrl, savePath)
}
} else {
console.log('No images found in the specified DOMs.')
}
await browser.close()
}
// This is the URL of the website we want to scrape the images
const websiteUrl = 'https://www.designkit.com/puzzle/'
scrapeFirstImagesFromReactWebsite(websiteUrl)