R语言 天猫评论爬虫_new

library(XML)
library(xml2)
library(rvest)
library(stringr)

library(jsonlite)

#找出需要爬的网址(F12进入,network+F5刷新寻找评论)


for(i in 1:99){
url[i] <- paste0("https://rate.tmall.com/list_detail_rate.htm?itemId=544568867374&spuId=719363051&sellerId=661559176&order=3¤tPage=",i,"&append=0&content=1")
}

#利用jsonlite包抓取
download <- function(url){
web <- read_html(url,encoding = "GBK")
# 抓取内容
List <- web %>% html_nodes("p") %>% html_text %>%
str_replace('\"rateDetail\":',"") %>% fromJSON()

# 获取评论页数
List$paginator

# 获取内容
details <- List$rateList

# 合并为数据框
data <- data.frame(
  User <- details$displayUserNick,
  Sku <- details$auctionSku,
  comment <- details$rateContent,
  rateDate <- details$rateDate
)
}

# 进度条功能
library("tcltk")
library("tcltk2")  #  这个包自定义进程形状更加好看

pb <- tkProgressBar("test progress bar", 

                                    "Some information in %",

                                     0, 100, 0)   #  最小,最大。起始位置
Sys.sleep(0.5)

for (i in 1:99){
  {
  info <- sprintf("程序已完成 %d%%", i)
  setTkProgressBar(pb, i, sprintf("test (%s)", info), info)
}
  if(i == 1){
    data <- download(url[i])
  }
  else{
    data1 <- download(url[i])
    data <- rbind(data,data1)
  }

}

# 进度条关闭

close(pb)

#写入文档

write.csv(data0,"D://Rtest/excel.csv")

你可能感兴趣的:(R语言)