我想下载推文(不搜索特定问题)。我试过你的建议:
curlPerform(url = https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -o "somefile.txt"
# 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",
cainfo = "cacert.pem",
write=WRITE_TO_FILE)
只有我压制'userpwd =“用户名:密码'我才会得到一个结果,这是一个包含以下信息的文本文件:
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1/statuses/sample.json'. Reason:
<pre> Unauthorized</pre>
我希望完全保留在R中并且需要使用Windows。关于如何解决这个问题的任何建议?
提前致谢
答案 0 :(得分:2)
尝试使用userpwd
参数指定用户名和密码:
library(RCurl)
WRITE_TO_FILE <- function(x) {
if (nchar(x) > 0) {
write.table(x, file='twitter_stream_capture.txt', append=TRUE,
row.names=FALSE, col.names=FALSE)
}
}
download.file(url='http://curl.haxx.se/ca/cacert.pem', destfile='cacert.pem')
getURL('https://stream.twitter.com/1/statuses/sample.json',
userpwd='username:password', cainfo='cacert.pem',
write=WRITE_TO_FILE)
使用有效的Twitter用户名和密码替换username
中的password
和getURL
。