Last active
December 25, 2015 00:49
-
-
Save gregeng/6890282 to your computer and use it in GitHub Desktop.
oop-studentscrape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'nokogiri' | |
require 'open-uri' | |
require_relative './student_scrape' | |
class Student | |
attr_accessor :name, :twitter, :linkedin, :facebook, :website | |
@@students = [] | |
def initialize(name, twitter, linkedin, facebook, website) | |
@@students << self | |
@name = name | |
@twitter = twitter | |
@linkedin = linkedin | |
@facebook = facebook | |
@website = website | |
end | |
def self.reset_all | |
@@students.clear | |
end | |
def self.all | |
@@students | |
end | |
def self.find_by_name(name) | |
@@students.select do |x| | |
x.name == name.to_sym | |
end | |
end | |
end | |
class StudentScraper | |
attr_accessor :url, :student | |
@@student_profiles_array = [] | |
def initialize(url) | |
@url = url | |
end | |
def call | |
student_index_page = Nokogiri::HTML(open(@url)) | |
self.scrape_index(student_index_page) | |
end | |
def scrape_index(student_index_page) | |
student_links = | |
student_index_page.css("div.blog-title div.big-comment h3 a").collect do |link| | |
"http://students.flatironschool.com/#{link['href']}" | |
end | |
self.scrape_pages(student_links) | |
end | |
def scrape_pages(student_links) | |
student_profiles_array = [] | |
student_links.each do |student| | |
begin | |
student_profile = Nokogiri::HTML(open(student)) | |
name = student_profile.css("h4.ib_main_header").text.to_sym | |
quote = student_profile.css("div#testimonial-slider").text.gsub("\n","").strip! | |
biography = student_profile.css("div.services p")[0].text.gsub("\n","").strip! | |
social_links = student_profile.css('div.social-icons a') | |
twitter = student_profile.css('div.social-icons a')[0].first[1] | |
linkedin = student_profile.css('div.social-icons a')[1].first[1] | |
github = student_profile.css('div.social-icons a')[2].first[1] | |
blog = student_profile.css('div.social-icons a')[3].first[1] | |
student_profiles_hash = {} | |
student_profiles_hash[name] = {} | |
student_profiles_hash[name][:quote] = quote | |
student_profiles_hash[name][:biography] = biography | |
student_profiles_hash[name][:social_links] = {} | |
student_profiles_hash[name][:social_links][:twitter] = twitter | |
student_profiles_hash[name][:social_links][:linkedin] = linkedin | |
student_profiles_hash[name][:social_links][:github] = github | |
student_profiles_hash[name][:social_links][:blog] = blog | |
name = Student.new(name, student_profiles_hash[name][:social_links][:twitter], student_profiles_hash[name][:social_links][:linkedin], student_profiles_hash[name][:social_links][:github], student_profiles_hash[name][:social_links][:blog]) | |
rescue | |
puts "#{student} just created an error" | |
end | |
end | |
end | |
end | |
class CLIStudent | |
end | |
binding.pry | |
url = "http://students.flatironschool.com" | |
student_scrape = StudentScraper.new(url) | |
student_hashes = student_scrape.call |
emilyxxie
commented
Oct 8, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment