R/sbscrape_twitter.R

#'Scrape monthly Twitter statistics from socialblade.com
#'
#'@param url will only work with https://socialblade.com/twitter/user/.../monthly links
#'
#'@keywords twitter
#'@concept socialblade
#'@export
#'@examples
#'sbscrape_twitter("https://socialblade.com/twitter/user/realdonaldtrump/monthly")


sbscrape_twitter<-function(url){

  # require(rvest,quietly = TRUE)
  # require(lubridate,quietly=TRUE)

  tryCatch(
    {
      pipeit<-function(url,code){
        read_html(url)%>%html_nodes(code)%>%html_text()
      }

      lk<-pipeit(url,"div:nth-child(11) div:nth-child(3) div:nth-child(2)")
      lk<-lk[grepl("[0-9]",lk)]

      tk<-pipeit(url,"div div div div:nth-child(4) div:nth-child(2)")
      tk<-tk[!grepl("[A-z]",tk)]

      tw<-pipeit(url,"div div div div:nth-child(5) div:nth-child(2)")
      tw<-tw[!grepl("[A-z]",tw)]
      tw<-tw[grepl("[0-9]",tw)]

      dates<-c()
      for(i in 1:30){
        dates[i]<-Sys.Date()-i+1
      }
      dates<-rev(as_date(dates))
      lk<-as.numeric(sapply(as.character(lk),
                            function(x) gsub(",","",x)))
      tk<-as.numeric(sapply(as.character(tk),
                            function(x) gsub(",","",x)))
      tw<-as.numeric(sapply(as.character(tw),
                            function(x) gsub(",","",x)))

      data.frame(Date=dates,"Followers"=lk,"Following"=tk,"Tweets"=tw)
    },
    error=function(error_message){
      message("Error in sbscrape_twitter: Please enter valid url. ")
      message("sbscrape will only work with https://socialblade.com/twitter/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.