準備工作 還沒載的快點載
library(dplyr)
library(rvest)
library(RCurl)
library(tm)
library(tmcn)
library(magrittr)
library(tidyr)
資料搜集
gos.data<-NULL
for(j in 29162:29172){
#第一步先進到八卦版,有18禁的限制要記得繞過去啊
#------------這部分是在不粉看板才需要加的(Gossiping Sex 之類的)---
curl <- getCurlHandle()# 不知道幹嘛
curlSetOpt(cookie="over18=1", followlocation = TRUE, curl=curl)#一樣問號
url <- paste0("https://www.ptt.cc/bbs/Gossiping/index",j,".html")#終於要放進來惹
gos <- getURL(url, curl=curl)#啊啊粗乃惹
#-----------------------------------------------------------------
#這裡先做一個data frame 把作者、推文數、日期、連結放在一起
gos.author<-read_html(gos) %>%html_nodes(".author") %>% html_text()
gos.push<-read_html(gos) %>%html_nodes(".nrec") %>% html_text()
gos.date<-read_html(gos) %>%html_nodes(".date") %>% html_text()
gos.link<- read_html(gos) %>% html_nodes(".title a") %>% html_attr('href')
a<-grep("-",gos.author)
if(length(gos.link)==length(gos.author)){
gos.data1=data.frame(gos.author=gos.author,gos.push=gos.push,gos.date=gos.date,
gos.link=gos.link)
}else{
gos.data1=data.frame(gos.author=gos.author[-a],gos.push=gos.push[-a],gos.date=gos.date[-a],
gos.link=gos.link)
}
gos.data=rbind(gos.data,gos.data1)
}
這裡稍微看一下資料
head(gos.data)
## gos.author gos.push gos.date gos.link
## 1 number5566 3 12/18 /bbs/Gossiping/M.1513526410.A.5E1.html
## 2 spector66 3 12/18 /bbs/Gossiping/M.1513526442.A.BB0.html
## 3 ashebird 3 12/18 /bbs/Gossiping/M.1513526468.A.301.html
## 4 F16V 7 12/18 /bbs/Gossiping/M.1513526537.A.AA3.html
## 5 kas310695 X1 12/18 /bbs/Gossiping/M.1513526553.A.3C0.html
## 6 actuy 8 12/18 /bbs/Gossiping/M.1513526586.A.06C.html
整理一下 “爆文”改成100 噓文改成-
gos.data$gos.push<-gsub("X","-",gos.data$gos.push)
gos.data$gos.push<-gsub("爆","100",gos.data$gos.push)
gos.data$gos.push%<>%as.numeric()
gos.data$gos.link %<>% as.character()
文字分析
#先想好要找哪些資料
#這裡搜的是推文數>10
links_ptt_gos<-gos.data %>% filter(gos.push>10) %>% select(gos.link)
links_ptt_gos<-links_ptt_gos[[1]]
ptt_data = c()
for(i in 1:length(links_ptt_gos)){
url <- paste0('https://www.ptt.cc',links_ptt_gos[i])
curl <- getCurlHandle()# 不知道幹嘛
curlSetOpt(cookie="over18=1", followlocation = TRUE, curl=curl)#一樣問號
url1 <- getURL(url, curl=curl)#啊啊粗乃惹
css<- read_html(url1) %>% html_nodes("#main-content") %>% html_text()
ptt_data=c(ptt_data,css)
#Sys.sleep(runif(1,2,5))
}
資料剪剪貼貼刪刪
rrow<-length(ptt_data)
tex<-paste(ptt_data[1:rrow],collapse = "")
texx<-strsplit(tex,split = "\n",fixed=T)
texx<-texx[[1]]
texx<-removePunctuation(texx)
texx<-removeNumbers(texx)
texx<-gsub("[a-zA-Z]","",texx)
texx<-gsub("※ 發信站 批踢踢實業坊 來自 ","",texx)
texx<-gsub("※ 文章網址 ","",texx)
texx<-gsub("推","",texx)
texx<-gsub("噓","",texx)
texx<-gsub("→","",texx)
texx %>% head()
## [1] "作者 會比緬因州還要熱。看板標題新聞 旬失智婦路上發抖 暖警忍度低溫背她時間 "
## [2] "旬失智婦路上發抖 暖警忍度低溫背她回派出所"
## [3] ""
## [4] ""
## [5] ""
## [6] ""
暴力分開他們>////<
row <- length(texx)
Gf <- paste(texx[1:row],collapse = "")
Gfinal <- gsub(" ","",Gf)
word = NULL
n = nchar(Gfinal)
for(i in 1:n-1){
word <- c(word, substr(Gfinal,i,i+1))
}
wordtable_i <- table(word)
wordtable <- sort(wordtable_i,decreasing = TRUE)
wordtable %>% head()
## word
## 台灣 不是 什麼 自己 就是 出來
## 68 49 49 45 43 40
Yoooooooooo