0

我是 R 编程或 Twitter API 的新手。

当我运行以下代码时:

#https://cran.r-project.org/web/packages/rtweet/index.html
#http://127.0.0.1:1410

#Twitter Data Analysis
twitter_packages <- c("rtweet","httpuv")
### checking if packages are already installed and installing if not
check.install.load.Package<-function(package_name){
  if(!package_name%in%installed.packages()){
    install.packages(package_name)

  }
  library(package_name,character.only = TRUE)
}
for(package in twitter_packages){
  check.install.load.Package(package)
}



## whatever name you assigned to your created app
#appname <- "Tech Dope"
appname <- "RTestApplicationSub"

## api key (example below is not a real key)
#key <- "XXXXXXXXXXXXX"
key <- "XXXXXXXXXXXXX"

## api secret (example below is not a real key)
#secret <- "XXXXXXXXXXXXX"
secret <- "XXXXXXXXXXXXX"

## create token named "twitter_token"
twitter_token <- create_token(
  app = appname,
  consumer_key = key,
  consumer_secret = secret)


india_india <- get_trends("India")

for(india in as.vector(india_india$trend)){
  gc()

  ## create token named "twitter_token"
  # twitter_token <- create_token(
  #   app = appname,
  #   consumer_key = key,
  #   consumer_secret = secret)

  fetchedTweets=search_tweets(india,10000,include_rts = FALSE,type = "mixed")
  write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\",india,".csv"), row.names = FALSE)
}

fetchedTweets=search_tweets("rohingya india",10000,include_rts = FALSE,type = "mixed")
write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\rohingya.csv"), row.names = FALSE)
class(fetchedTweets)

#write.csv(fetchedTweets,"C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\Aadhar.csv")

我收到此错误:

Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Error in x[[1]] : subscript out of bounds
In addition: Warning messages:
1: rate limit exceeded. 
2: rate limit exceeded. 

现在根据 twitter API 文档 API 调用仅限于请求/15 分钟窗口(用户身份验证)180 请求/15 分钟窗口(应用程序身份验证)450

但就我而言,在 search_tweets() 被调用 6 次后,我得到了“超出速率限制”。

我可以通过将循环重写为:

i=1;
for(india in as.vector(india_india$trend)){
  gc()
  i<-i+1
  if(i%%5==0){
    Sys.sleep(600)
  }
  ## create token named "twitter_token"
  # twitter_token <- create_token(
  #   app = appname,
  #   consumer_key = key,
  #   consumer_secret = secret)

  fetchedTweets=search_tweets(india,10000,include_rts = FALSE,type = "mixed")
  write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\",india,".csv"), row.names = FALSE)
}

我的问题如下: 1)对于每个 search_tweets() 调用,对 Twitter 的 API 调用有多少次?2)它是否取决于获取的推文数量?

4

0 回答 0