SW0000J

app.js 연합뉴스 야구기사 크롤링

Showing 1 changed file with 33 additions and 0 deletions
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://www.yna.co.kr/sports/baseball");
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 = $("ul.list li").children("div.item-box01");
18 +
19 + $bodyList.each(function(i, elem) {
20 + ulList[i] = {
21 + datetime: $(this).find('span.txt-time').text(),
22 + image_url: $(this).find('figure.img-con a').attr('href'),
23 + url: $(this).find('div.news-con a').attr('href'),
24 + title: $(this).find('div.news-con strong').text(),
25 + summary: $(this).find('div.news-con p').text().slice(0, -29)
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