对于每个search_tweets()调用调用twitter的API调用次数,是否取决于提取的推文数量?

时间:2017-09-17 19:15:56

标签: r twitter rate-limiting ratelimit rtweet

我是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分钟窗口(app auth)450

但在我的情况下,我已超过&#34;超出限额&#34;在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()调用是否会调用多少次API调用? 2)是否取决于提取的推文数量?

0 个答案:

没有答案