Twitter ruby​​ gem .search请求多个回复

时间:2015-06-09 07:09:22

标签: ruby twitter twitter-gem

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

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

1 个答案:

答案 0 :(得分:1)

正如文档所述,

  

:count(整数) - 每页返回的推文数量,最多为1   最多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