반응형
const axios = require("axios"); //특정 URL 삽입 시 URL html 태그 가지고
const cheerio = require("cheerio");
var scanf = require('scanf');
// HTML 코드를 가지고 오는 함수
const getHTML = async(keyword) => {
try{
return await axios.get("https://search.naver.com/search.naver?where=news&ie=UTF-8&query=" + encodeURI(keyword)) //""안에는 URL 삽입
}catch(err) {
console.log(err);
}
}
// 파싱 함수
const parsing = async (keyword) => {
const html = await getHTML(keyword)
const $ = cheerio.load(html.data);// 가지고 오는 data load
const $titlist = $(".news_area");
let informations = [];
$titlist.each((idx,node) => {
const title = $(node).find(".news_tit").text();
informations.push({
title: $(node).find(".news_tit:eq(0)").text(), // 뉴스제목 크롤링
press: $(node).find(".info_group > a").text(), // 출판사 크롤링
time: $(node).find(".info_group > span").text(), // 기사 작성 시간 크롤링
contents: $(node).find(".dsc_wrap").text(), // 기사 내용 크롤링
})
console.log(informations);
}); //for문과 동일
}
parsing("도지코인"); // 검색어
반응형
'JSP' 카테고리의 다른 글
JSP 무작정 해보기 - 로그아웃 페이지, 메인 페이지 틀 잡기 (1) | 2019.11.05 |
---|---|
JSP 무작정 해보기 - 사용자에게 세션 부여하기 (0) | 2019.11.05 |
JSP 무작정 해보기 - 회원가입 (0) | 2019.11.05 |
JSP무작정 해보기 - 게시판 만들기 - DB 만들기, DB 연동, 로그인 함수 (0) | 2019.11.03 |
JSP무작정 해보기 - 게시판 만들기 - Login.jsp (0) | 2019.10.31 |