require 'mechanize' fb_friends = [] # login agent = Mechanize.new page = agent.get("https://www.facebook.com") next_page = page.form_with(:id => 'login_form') do |form| form.field_with(:id => 'email').value = 'username' form.field_with(:id => 'pass').value = 'password' end.submit i = 0 while true do page2 = agent.get("https://touch.facebook.com/profile.php?v=friends&startindex=#{i}") if page2.search("h3 a").count != 0 page2.search("h3 a").each do |link| fb_friends << link.children.first.text end i = i + page2.search("h3 a").count else break end end fb_friends = fb_friends.uniq