Skip to content

Instantly share code, notes, and snippets.

@terrbear
Created March 25, 2009 16:00
Show Gist options
  • Save terrbear/85546 to your computer and use it in GitHub Desktop.
Save terrbear/85546 to your computer and use it in GitHub Desktop.
#here's what the scrape method looks like
def scrape!(&block)
next_link = nil
self.logged_in do
loop do
break if next_link == "none"
response = next_link ?
self.http_get(next_link) :
response = self.http_post(self.results_url, @conditions)
next_link = find_next_link(response.body)
continue = yield self.create_results(response.body)
break unless continue
end
end
end
#here's how some code working with a similar convention
#should work
DATA = [
{:name => "Bob Sagat", :age => 53},
{:name => "Terry Heath", :age => 25},
{:name => "Jeremiah Marcum", :age => 27}
]
def scrape!(&block)
index = 0
loop do
guy = DATA[index]
break if guy.nil?
continue = yield guy
index += 1
break unless continue
end
end
def scrape_all
#output = Bob Sagat, Terry Heath, Jeremiah Marcum
results = []
scrape! do |guy|
#in your case, guy would be an array of results
results << guy
true
end
puts "scrape all results! #{results.map{|x| x[:name]}.join(', ')}"
end
def scrape_2
#output = Bob Sagat, Terry Heath
results = []
scrape! do |guy|
results << guy
results.size < 2
end
puts "scrape 2 results! #{results.map{|x| x[:name]}.join(', ')}"
end
scrape_all
scrape_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment