CSR(client side rendering) 사이트 크롤링 하기
마스터욱
0
15
0
0
2024-06-10 16:59:28
const express = require('express');
const fs = require('fs');
const { exec } = require('child_process');
const app = express();
app.get('/render', (req, res) => {
exec('node render.js', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return res.status(500).send('Error rendering page');
}
const content = fs.readFileSync('rendered.html', 'utf8');
res.send(content);
});
});
app.get('/', (req, res) => {
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.goto(크롤링URL, { waitUntil: 'networkidle2' });
const content = await page.content();
// 렌더링된 HTML을 파일로 저장
//fs.writeFileSync('rendered.html', content);
await browser.close();
res.send(content);
})();
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});