Twitter ruby gem .search 请求多个响应

Twitter ruby gem .search request multiple responses

在下面的代码中,我指定我想向 twitter api 发出 1 个请求,但是当我 puts 输出到终端时,我继续收到很多响应,直到我终止程序.如何将其限制为仅指定数量的请求?我是 ruby 的新手,所以我可能在这里明显遗漏了一些东西。

Docs to the twitter gem api

require 'twitter'

...

client = Twitter::REST::Client.new do |config|

  config.consumer_key        = credentials[:consumer_key]
  config.consumer_secret     = credentials[:consumer_secret]
  config.access_token        = credentials[:access_token]
  config.access_token_secret = credentials[:access_token_secret]

end

search_results = client.search "China", {:count => 1, :lang => 'en', :result_type => 'recent'}

search_results.each do |value|

    puts value.user.screen_name

end

如文档所述,

:count (Integer) — The number of tweets to return per page, up to a maximum of 100.

所以,这与您要实现的目标有点相反:count 越多,您发出的 http 请求就越少(因为每个请求都包含更多推文)。请注意,您不能将 count 设置为超过 100 的数量,因此在有大量推文的情况下限制请求数量的唯一方法是

search_results.take(requests_amount)
# => made requests_amount requests, returns requests_amount pages
# each of page should contain up to count tweets, with max of 100