Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Last active December 22, 2015 07:39
Show Gist options
  • Save rskelley9/6439807 to your computer and use it in GitHub Desktop.
Save rskelley9/6439807 to your computer and use it in GitHub Desktop.
In progress.
# Solution for Challenge: Scraping HN 1: Building Objects. Started 2013-09-04T16:13:44+00:00
require 'nokogiri'
require 'open-uri'
class Post
attr_reader :title, :url, :points, :item_id
def initialize((post),title, url, points, item_id)
@comments = []
@title = title
@url = url
@points = points
@item_id = item_id
end
def comments(doc)
@comments = doc.search('.comment > font:first-child').map { |font| font.inner_text}
end
def add_comment(comment)
@comments << comment
end
end
class Comment
def initialize(name, points, text)
@name = name
@points = points
@text = text
end
def text
@text
end
doc = Nokogiri::HTML(open('post.html'))
title = doc.search('.title > a:first-child').map { |link| link.inner_text}
comments = doc.search('.comment > font:first-child').map { |font| font.inner_text}
url = doc.search('.title > a:first-child').map { |link| link['href']}
it_id = doc.search('.subtext > a:nth-child(3)').map {|link| link['href'] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment