rvest 라이브러리 설치 및 임포트
install.packages("rvest")
library(rvest)
# 만약 오류가 나면 iconv로 인코딩 하면된다.
# UTF-8로 되어있으면 문제 없음
html <- read_html(iconv("주소", from = 'euc-kr',to='cp949'),encoding='cp949')
# character 인코딩 체계가 어떻게 되어있는지 확인(확률값)
guess_encoding(html)
# xpath로 텍스트 뽑아내기
html_nodes(html,xpath='//*[@id="old_content"]/table/tbody/tr/td[2]/text()')
예시 : 인공지능 신문 - 의료 AI 기사 크롤링
# html 주소 불러오기 html <- read_html("http://www.aitimes.kr/news/articleList.html") |
![]() |
# url 저장 url <- html_nodes(html,".list-titles")%>% |
![]() |
# 기사 내용 불러오기 news <- c() for(i in 1:length(url)){ html <- read_html(paste0("http://www.aitimes.kr",url[i])) text <- html_node(html,'#article-view-content-div')%>% html_text() news <- c(news,text) } |
![]() |
# 기사 내용 정제작업 t <- c() for(i in 1:length(news)){ x22 <- SimplePos22(news[i]) x22 str_match(x22, "[A-z가-힣]+/N") word_nn <- as.vector(na.omit(str_match(x22, "([A-z가-힣]+)/N")[,2])) t <- c(t,word_nn) word <- table(t) } |
![]() |
# 워드클라우드로 띄워보기 wordcloud2(word) |
![]() |
'컴퓨터 > R' 카테고리의 다른 글
R - 크롤링 연습 ② (0) | 2020.04.27 |
---|---|
R - 크롤링 연습 ① (0) | 2020.04.27 |
R - KoNLP 설치 및 사용 (0) | 2020.04.25 |
R - stringr을 이용한 텍스트 정제 작업 (0) | 2020.04.23 |
R - wordcloud (0) | 2020.04.23 |