[Node.js]웹페이지 크롤링

Updated:

[코드]


const axios = require("axios"); 
const cheerio = require("cheerio"); 
const fs = require('fs');
const log = console.log; 


var file = 'temp_text.txt';
fs.open(file, 'w', function(err, fd){
if(err) throw err;
console.log('file open complete');
});

const getHtml = async () => { 
    try { 
        return await axios.get("https://kimmusic.github.io/"); 
    } 
        catch (error) { console.error(error); } }; 
        getHtml() 
        .then(html => { 
            let ulList = []; 
            const $ = cheerio.load(html.data); 
            const $bodyList = $("div.entries-list").children("div.list__item"); 
            $bodyList.each(function(i, elem) { 
                ulList[i] = { 
                    title: $(this).find('article p.archive__item-excerpt').text()};

                }); 
                for(var i = 0; i < ulList.length; ++i)
                    fs.appendFileSync('temp_text.txt', ulList[i].title + '\n', 'utf-8', function(error){
                    console.log(k + 'error');
                });
                const data = ulList.filter(n => n.title);

                return data;

            }) .then(res => log(res));






참고한 블로그 : https://velog.io/@yesdoing/Node.js-%EC%97%90%EC%84%9C-%EC%9B%B9-%ED%81%AC%EB%A1%A4%EB%A7%81%ED%95%98%EA%B8%B0-wtjugync1m
https://mine7717.tistory.com/11

태그:

카테고리:

업데이트:

댓글남기기