我刚刚开始使用Twitter Streaming API并使用命令行,使用以下命令将原始JSON响应重定向到文件:
curl https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -o "somefile.txt"
是否可以完全保留在R中并利用RCurl来做同样的事情?我想解析返回的每个响应,而不是仅仅将输出保存到文件中.我过去曾解析过Twitter搜索结果,但我想在收到每个回复时这样做.实质上,将函数应用于每个JSON响应.
提前致谢.
编辑:这是我在R中尝试过的代码(不幸的是我在Windows上).我需要包含对.pem文件的引用以避免错误.但是,代码只是"运行",我似乎无法看到返回的内容.我试过打印,猫等.
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
getURL("https://stream.twitter.com/1/statuses/sample.json", 
       userpwd="USER:PWD",
       cainfo = "cacert.pem")
我能够弄清楚基础知识,希望这会有所帮助.
#==============================================================================
# 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)
| 归档时间: | 
 | 
| 查看次数: | 4652 次 | 
| 最近记录: |