Connect to the Twitter Streaming API using R

后端 未结 2 1692
终归单人心
终归单人心 2021-01-06 22:30

I just started playing around with the Twitter Streaming API and using the command line, redirect the raw JSON reponses to a file using the command below:

cu         


        
相关标签:
2条回答
  • 2021-01-06 23:02

    Try the twitter api package for R.

    install.packages('twitteR')
    library(twitteR)
    

    I think this is what you need.

    0 讨论(0)
  • 2021-01-06 23:04

    I was able to figure out the basics, hopefully this helps.

    #==============================================================================
    # Streaming twitter using RCURL
    #==============================================================================
    library(RCurl)
    library(rjson)
    
    # set the directory
    setwd("C:\\")
    
    #### redirects output to a file
    WRITE_TO_FILE <- function(x) {
    
         if (nchar(x) >0 ) {
              write.table(x, file="Twitter Stream Capture.txt", append=T, row.names=F, col.names=F)
         }
    
    }
    
    ### windows users will need to get this certificate to authenticate
    download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
    
    ### write the raw JSON data from the Twitter Firehouse to a text file
    getURL("https://stream.twitter.com/1/statuses/sample.json", 
           userpwd=USER:PASSWORD,
           cainfo = "cacert.pem", 
           write=WRITE_TO_FILE)
    
    0 讨论(0)
提交回复
热议问题