linaaaaa1224 / 2020----

0 stars 0 forks source link

<10주차 과제-1> 1917 리뷰 #1

Open linaaaaa1224 opened 3 years ago

linaaaaa1224 commented 3 years ago

setwd("C:/")#워크디렉토리 설정

install.packages(c('httr','rvest','stringr','tm','sna','xml2'))#관련 패키지 다운로드

library(httr); library(rvest); library(stringr); library(tm); library(sna); library(xml2) #라이브러리로 업로드

all.reviews <- c() #리뷰 저장공간_만들기_1 reviews <- c() #리뷰 저장공간_만들기_2

10 page loop

for(i in 1:10) { url <- paste0("https://movie.naver.com/movie/point/af/list.nhn?st=mcode&sword=187321&target=after&page=",i) res <- GET(url) #이 과정을 거쳐야 원활하게 되더라구요~ Get 방식 , post 방식

https://statkclee.github.io/yonsei/data/R_Web_Crawling.pdf

h <- read_html(res, encoding = "CP949") #한글 처리할 때 늘 만나는 문제(인코딩: https://studyforus.tistory.com/167 ) table <- html_nodes(h, '.list_netizen') #리뷰가 있는 위치 찾아 들어가기 content <- html_nodes(table, '.title') reviews <- html_text(content) #실제 리뷰의 text 파일만 추출 if( length(reviews) == 0 ){ break } reviews <- str_trim(reviews) #앞뒤 공백문자 제거 all.reviews <- c(all.reviews, reviews) #결과값 저장 print(i) }

all.reviews #잘 들어왔나 확인

write.table(all.reviews, '1917.txt') #txt 파일로 저장하기