-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (62 loc) · 2.06 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
59
60
61
62
63
64
65
66
67
68
const puppeteer = require('puppeteer')
const fs = require('fs')
const axios = require('axios')
const imageType = require('image-type')
//https://www.penup.com/main/home
const penupLink = 'https://www.penup.com/main/home'
const windowWidth = 1920
const windowHeight = 1080
const folderName = `images`
const MAX_SCROLL = 2;
fs.readdir(folderName, (err) => {
if (err) {
console.error(`${folderName} 폴더가 없어 ${folderName} 폴더를 생성합니다.`);
fs.mkdirSync(folderName);
}
});
const penupCrawler = async () => {
const browser = await puppeteer.launch({
headless: false,
args: [
'--no-sandbox',
`--window-size=${windowWidth},${windowHeight}`]
});
const pages = await browser.newPage()
await pages.setViewport({width: windowWidth, height: windowHeight})
await pages.goto(penupLink)
let imageList = []
for(let scrollCount = 0; scrollCount < MAX_SCROLL; scrollCount++ ) {
await pages.waitForSelector('.grid-item')
const srcs = await pages.evaluate(() => {
window.scrollTo(0,0)
const imageList = []
const delList = []
const imageEls = document.querySelectorAll('.grid-item')
imageEls && imageEls.forEach((eachEls) => {
const currentImageLink = window.getComputedStyle(eachEls.querySelector(`.artwork-image`))['backgroundImage']
if(currentImageLink && currentImageLink != 'none') {
imageList.push(currentImageLink.match(/(?<=\(").+?(?="\))/)[0])
delList.push(eachEls)
}
})
// delete node
delList.forEach(eachEl => {
eachEl.parentNode.removeChild(eachEl)
})
window.scrollBy(0,500)
return imageList
})
imageList = imageList.concat(srcs)
}
console.log(imageList)
imageList.forEach(async (eachUrl) => {
const imgResult = await axios.get(eachUrl, {
responseType: 'arraybuffer',
});
const { ext } = imageType(imgResult.data);
fs.writeFileSync(`${folderName}/${Math.floor(new Date().getTime())}.${ext}`, imgResult.data);
})
await pages.close()
await browser.close()
}
penupCrawler()