问题描述
我刚刚开始使用Twitter Streaming API,并使用命令行,使用以下命令将原始JSON响应重定向到一个文件:
curl https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -osomefile.txt
是否可以完全停留在R中并利用RCurl做同样的事情?而不是只是将输出保存到文件,我想解析返回的每个响应。我已经解析过twitter搜索结果,但我想这样做,因为每个响应都收到。基本上,对每个JSON响应应用一个函数。
提前感谢。
我试过在R(我在Windows上,不幸的是)。我需要包括对.pem文件的引用,以避免错误。但是,代码只是运行,我似乎不能看到返回的东西。我已尝试过打印,cat等。
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)
我能够找出基本信息,希望这有助于。
#====== ================================================== ====================
#使用RCURL串流微博
#================ ================================================== ============
库(RCurl)
库(rjson)
#设置目录
setwd(C: \\\)
####将输出重定向到文件
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用户需要获取此证书来验证
download.file(url =http://curl.haxx.se/ca/cacert。 pos,destfile =cacert.pem)
###将原始JSON数据从Twitter Firehouse写入文本文件
getURL(https://stream.twitter。 com / 1 / statuses / sample.json,
userpwd = USER:PASSWORD,
cainfo =cacert.pem,
write = WRITE_TO_FILE)
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:
curl https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -o "somefile.txt"
Is it possible to stay completely within R and leverage RCurl to do the same thing? Instead of just saving the output to a file, I would like to parse each response that is returned. I have parsed twitter search results in the past, but I would like to do this as each response is received. Essentially, apply a function to each JSON response.
Thanks in advance.
EDIT: Here is the code that I have tried in R (I am on Windows, unfortunately). I need to include the reference to the .pem file to avoid the error. However, the code just "runs" and I can not seem to see what is returned. I have tried print, cat, etc.
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")
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)
这篇关于使用R连接到Twitter Streaming API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!