ruby 针对复杂网络的twitter爬虫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 针对复杂网络的twitter爬虫相关的知识,希望对你有一定的参考价值。

require 'rspec'
require 'twitter'

class TwitterUser 
  def initialize()
    @twitter = Twitter.new
  end

  def get_user(user_name)
    begin
      return @twitter.user(user_name)
    rescue
    end
  end

  def get_following(user_name)
    begin
      return @twitter.friend_ids(user_name)
    rescue
    end
  end

  def get_followers(user_name)
    begin
      return @twitter.follower_ids(user_name)
    rescue
    end
  end
end

class TwitterCrawler
  def initialize(start_user)
    @twitter_user = TwitterUser.new
    @start_user_id = Twitter.user(start_user)["id"]
    end

  def following_relation
    @relation_array = Array.new

    @following_ids = @twitter_user.get_following(@start_user_id)["ids"]
    @following_ids.map{|id|
      @relation_array << [@start_user_id,id]
    }
  end
end


array = TwitterCrawler.new("onodes").following_relation[0]
f = File.open("following.dat","w")


array.each{|id_list|
  begin
  TwitterCrawler.new(id_list[1]).following_relation[0].map{|ids|
    f.puts(ids[0].to_s + " " + ids[1].to_s)
  }
  rescue
    next
  end
}
f.close

#p TwitterCrawler.new.get_user("onodes")
#p TwitterCrawler.new.get_followers("onodes")

describe do
  before do
    @twitter = TwitterUser.new.should be_true
  end

  it do
    @twitter.get_user("onodes").should be_true
  end

  it do
    @twitter.get_user(17942457).should be_true
  end

  it do
    @twitter.get_user("onodes").should eq @twitter.get_user(17942457)
  end
end

以上是关于ruby 针对复杂网络的twitter爬虫的主要内容,如果未能解决你的问题,请参考以下文章

[Python3网络爬虫开发实战] 1.5.4-RedisDump的安装

爬虫系统基础框架 & 何时使用爬虫框架?

Ruby和python哪个更易懂、灵活?

ruby 感谢跟随我使用ruby的twitter bot

ruby Twitter Ads API入门(Ruby)

ruby Twitter Ads API入门(Ruby)