R/sbscrape_youtube.R

#'Scrape monthly Youtube statistics from socialblade.com
#'
#'
#'@param url will only work with https://socialblade.com/youtube/user.../monthly links
#'
#'@keywords youtube socialblade
#'@export
#'@examples
#'sbscrape_youtube("https://socialblade.com/youtube/user/pewdiepie/monthly")

sbscrape_youtube<-function(url){
  # require(rvest,quietly = TRUE)
  # require(lubridate,quietly=TRUE)
  tryCatch(
    {
      pipeit<-function(url,code){
        read_html(url)%>%html_nodes(code)%>%html_text()
      }

      cv<-pipeit(url,
                 "div div div div div:nth-child(4) div:nth-child(2)")
      cv<-cv[-c(1:3)]
      cs<-pipeit(url,
                 "div div div:nth-child(1) div div:nth-child(3) div:nth-child(2)")
      cs<-cs[-c(1:3,34)]
      dates<-c()
      for(i in 1:30){
        dates[i]<-Sys.Date()-i+1
      }
      dates<-rev(as_date(dates))
      cv<-as.numeric(sapply(as.character(cv),
                            function(x) gsub(",","",x)))
      cs<-as.numeric(sapply(as.character(cs),
                            function(x) gsub(",","",x)))
      ern<-pipeit(url,"div div div div div:nth-child(5)")
      ern<-ern[sapply(ern,function(x)grepl("\\$",x))]
      ern<-sapply(ern,function(x)gsub("\n","",x))
      data.frame(Date=dates,"Culmulative Subs"=cs,"Culmulative Views"=cv,"Estimated Earnings"=ern)
    },
    error=function(error_message){
      message("Error in sbscrape_youtube: Please enter valid url. ")
      message("sbscrape will only work with https://socialblade.com/youtube/user/.../monthly links")
    })
}

Try the sbscrapeR package in your browser

Any scripts or data that you put into this service are public.

sbscrapeR documentation built on June 4, 2019, 5:03 p.m.