Showing
1 changed file
with
33 additions
and
0 deletions
testcode/test1.js
0 → 100644
1 | +const axios = require("axios"); | ||
2 | +const cheerio = require("cheerio"); | ||
3 | +const log = console.log; | ||
4 | + | ||
5 | +const getHtml = async () => { | ||
6 | + try { | ||
7 | + return await axios.get("https://sports.news.naver.com/kbaseball/news/index.nhn?isphoto=N&type=latest"); | ||
8 | + } catch (error) { | ||
9 | + console.error(error); | ||
10 | + } | ||
11 | +}; | ||
12 | + | ||
13 | +getHtml() | ||
14 | + .then(html => { | ||
15 | + let ulList = []; | ||
16 | + const $ = cheerio.load(html.data); | ||
17 | + const $bodyList = $("div.news_list ul").children("li"); | ||
18 | + | ||
19 | + $bodyList.each(function(i, elem) { | ||
20 | + ulList[i] = { | ||
21 | + url: $(this).find('a').attr('href'), | ||
22 | + image_url: $(this).find('a.thmb img').attr('src'), | ||
23 | + title: $(this).find('div.text a').text(), | ||
24 | + summary: $(this).find('div.text p').text(),//.slice(0, -29) | ||
25 | + datetime: $(this).find('div.text div.source span').text() | ||
26 | + }; | ||
27 | + console.log(ulList[i]) // list object checking code | ||
28 | + }); | ||
29 | + | ||
30 | + const data = ulList.filter(n => n.title); | ||
31 | + return data; | ||
32 | + //return ulList; | ||
33 | + }).then(res => console.log(res)); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment