r语言爬取任意商品评论

怎么用r语言获取商品评论啊

 

library(RCurl)
getcoments <- function(i){
    productid <- '2967929'  #商品id
    t1 <- 'http://club.jd.com/comment/productPageComments.action?productId='
    t2 <- '&score=0&sortType=1&page='  #按时间顺序
    t3 <- '&pageSize=1' #设置每页1条评论
    url <-paste0(t1,productid,t2,i,t3)
    web <- getURL(url, .encoding = 'gbk')
    comments <- substr(web,regexpr("comments", web)+10,regexpr("referenceTime", web)-4)
    content <- substr(comments,regexpr("content", comments)+10,regexpr("creationTime", comments)-4)
}

comment <- c()
n <- 30 #爬取评论条数
for(i in 0:(n-1)){
    comment <- rbind(comment,getcoments(i))
    print(i+1)
    Sys.sleep(1)
}
write.csv(comment,'jingdongcomment.csv')

 

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632