找到你要的答案

Q:Twitter gem search results pagination

Q:推特宝石的搜索结果分页

How I can paginate SearchResults data with "search" method. For example fetch results by 10 tweets per page. Thanks.

我怎么可以将搜索结果数据与“搜索”的方法。例如,每页10个推送结果。谢谢.

answer1: 回答1:

You don't necessarily need to deal with search result pages at all. As Twitter::SearchResults includes the Twitter::Enumerable module (which in turn includes Enumerable), you can just use something like results.take(20) to get up to 20 tweets. The module handles pagination automatically in the background for you.

That said, you can still tell twitter how many results it should return per page. You can pass the :count option to the search method like this:

client.search("to:justinbieber marry me", count: 20).take(30).each do |tweet|
  puts tweet.text
end

According to the API documentation, you can set any value there up to 100.

你不一定需要处理搜索结果页在所有。作为推特::::搜索结果包括推特枚举模块(其中又包括枚举),你可以使用类似的结果。把(20)到20条。该模块处理分页后台自动为你。

也就是说,你仍然可以告诉Twitter每个页面应该返回多少个结果。您可以将计数选项传递给搜索方法,例如:

client.search("to:justinbieber marry me", count: 20).take(30).each do |tweet|
  puts tweet.text
end

根据API文档,您可以设置任何值高达100。

ruby  twitter  gem