Skip to content

Instantly share code, notes, and snippets.

@irmiller22
Created February 23, 2014 21:41
Show Gist options
  • Save irmiller22/9177727 to your computer and use it in GitHub Desktop.
Save irmiller22/9177727 to your computer and use it in GitHub Desktop.
Kickstarter_scrape
# Kickstarter Project Scrape
require 'nokogiri'
# HTML, XML parser that allows us to view HTML code as series of nodes; able to highlight elements via CSS selectors
require 'open-uri'
# Module that allows you to make http request, and returns us the HTML content of a URL via the `open` command
require 'pry'
# projects
# kickstarter.css("li.project.grid_4")
# title
# project.css("h2.bbcard_name strong a").text
# image_link
# project.css("div.project-thumbnail a img").attribute("src").value
# description
# project.css("p.bbcard_blurb").text
# location
# project.css("ul.project-meta span.location-name").text
# percent_funded
# project.css("ul.project-stats li.first.funded strong").text --> .gsub("%", "").to_i
# 1 - projects hash
# 2 - name of project as the key
### want this to be converted to a symbol
# 3 - project attributes as the values stored within a hash
### key stored as a symbol
# 4 - image_link setup
# 5 - description setup
# 6 - location setup
# 7 - percent_funded setup
# projects = {
# :the_penguin_factory => {
# :image_link => "http://www.penguins.com/",
# :description => "A place where dreams come true",
# :location => "Paradise Bay, Antarctica",
# :percent_funded => 10
# }
# }
# projects[:the_penguin_factory][:location] = "Paradise Bay, Antarctica"
html = File.read('fixtures/kickstarter.html')
kickstarter = Nokogiri::HTML(html)
projects = Hash.new
kickstarter.css("li.project.grid_4").each do |project|
title = project.css("h2.bbcard_name strong a").text.to_sym
projects[title] = {
:image_link => project.css("div.project-thumbnail a img").attribute("src").value,
:description => project.css("p.bbcard_blurb").text,
:location => project.css("ul.project-meta span.location-name").text,
:percent_funder => project.css("ul.project-stats li.first.funded strong").text.gsub("%", "").to_i
}
end
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment