scraper.js 1.39 KB
Newer Older
baesangjune's avatar
baesangjune committed
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
import cheerio from "cheerio"
import express from 'express'
import request from 'request'
import axios from 'axios'

const app = express()


// axios를 활용해 AJAX로 HTML 문서를 가져오는 함수 구현

// getHTML 함수 실행 후 데이터에서
// body > main > div > section > ul > li > article > h2 > a
// 에 속하는 제목을 titleList에 저장
app.get('/', (req, res) => {
    const url = "https://section.blog.naver.com/Search/Post.nhn?pageNo=1&rangeType=ALL&orderBy=sim&keyword=%ED%95%9C%EB%9D%BC%EC%82%B0"
    request(url,function(err, res, html) {
        console.log(html)
        if(!err){
            let $=cheerio.load(html);
        }
    }
        // .then(html => {
        //     let titleList = [];
        //     const $ = cheerio.load(html.data);
        //     // ul.list--posts를 찾고 그 children 노드를 bodyList에 저장
        //     const bodyList = $(".title_post > span");

        //     // bodyList를 순회하며 titleList에 h2 > a의 내용을 저장
        //     bodyList.each(function (i, elem) {
        //         titleList[i] = {
        //             title: $(this)
        //                 .text()
        //         };
        //     });
        //     return titleList;
        // })
        // .then(res => console.log(res)); // 저장된 결과를 출력
    )
})

app.listen(3001, () => {
    console.log('Server is listening on port 3001')
})