반응형
2016/02/23 - [Data Analysis/R] - [R] Daum 영화 평점을 가져와서 분석하기 - 1
구조가 파악이 됐으면 구조에 따라 필요한 정보를 가져와 추출한다.
그림에서와같이 Comment의 경우 <span class="comment article"> 밑에 있는데 이 경우 rvest의 html_node()를 활용하여 해당 부분을 추출할 수 있다.
html_node('span.comment')
이를 이용하여 다음과 같이 스크립트를 구성할 수 있다.
library(rvest) reviews <- c() score <- c() for (i in 1:10) { url <- paste0('http://movie.daum.net/moviedetail/moviedetailNetizenPoint.do?movieId=92107&searchType=all&type=after&page=', i) htxt <- read_html(url) # comments <- html_nodes(htxt, 'span.comment') %>% html_nodes('a') %>% html_text() reviews <- c(reviews, comments) starwraps <- html_nodes(htxt, 'span.starWrap') %>% html_nodes('em') %>% html_text() score <- c(score, starwraps) } Encoding(reviews) <- 'UTF-8' df <- data.frame(reviews = reviews, score = score, stringsAsFactors=F)
가져온 df 를 활용하여 간단하게 wordcloud를 그린다.
library(KoNLP) library(wordcloud) library(RColorBrewer) words <- unlist(lapply(df$reviews, extractNoun)) wordcount <- data.frame(table(words), stringsAsFactors=F) mypalette <- brewer.pal(12, "Paired") # 폰트 세팅 windowsFonts(malgun=windowsFont("맑은 고딕")) wordcloud(wordcount$words, wordcount$Freq, min.freq=2, color=mypalette)
결과는 다음과 같다.
'Data Analysis > R' 카테고리의 다른 글
[R] NAVER 주식 DATA 가져오기-1 (1) | 2020.05.07 |
---|---|
[R] Apply 함수를 알아보자 (0) | 2020.04.21 |
[R] Daum 영화 평점을 가져와서 분석하기 - 1 (2) | 2016.02.23 |
[R] R 배치 프로그램 (2) | 2016.01.07 |
R : Data.table 공부 (0) | 2015.12.06 |
댓글