加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用ruby twitter gem获取大量推文

发布时间:2020-12-17 02:28:37 所属栏目:百科 来源:网络整理
导读:我写了一些 ruby来返回包含一个时间范围内的短语的所有推文.但是,此代码最多可返回1,500条推文.如何获得超过1,500条推文? (我希望得到成千上万的推文) require "rubygems"require "twitter" # returns a list of tweets containing the phrase within the d
我写了一些 ruby来返回包含一个时间范围内的短语的所有推文.但是,此代码最多可返回1,500条推文.如何获得超过1,500条推文? (我希望得到成千上万的推文)

require "rubygems"
require "twitter"

    # returns a list of tweets containing the phrase within the dates specified
    # returns either @max_tweets tweets or all tweets found
    # @param phrase - a phrase to search for
    # @param from_date - begining date of the search ex."2011-02-28"
    # @param until_date - ending date of the search ex. "2011-03-01"
    def get_tweets(phrase,from_date,until_date)

      search = Twitter::Search.new.containing(phrase).since_date(from_date).until_date(until_date)

      #get all the tweets
      tweets = search.fetch
      next_tweets = search.fetch_next_page
      while(tweets.size < @max_tweets && next_tweets != nil) 
        tweets = tweets + next_tweets
        next_tweets = search.fetch_next_page
      end

      return tweets.first(@max_tweets)
    end

解决方法

Twitter API docs状态

rpp
The number of tweets to return per page,up to a max of 100.
http://search.twitter.com/search.json?rpp=100

page
The page number (starting at 1) to return,up to a max of roughly 1500 results (based on rpp * page).
http://search.twitter.com/search.json?page=10

因此看起来1500是内置限制.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读