Skip to content

Instantly share code, notes, and snippets.

@malditogeek
Created November 19, 2010 12:03
Show Gist options
  • Save malditogeek/706433 to your computer and use it in GitHub Desktop.
Save malditogeek/706433 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'open-uri'
require 'pp'
# User provided Book Depository wishlist.
wishlist_url = 'http://www.bookdepository.com/wishlist/507838/Mauro-Pompilio'
# Fectch the wishlisted books.
def fetch_books(wishlist_url, page=1)
doc = Nokogiri::HTML(open("#{wishlist_url}/?page=#{page}"))
doc.xpath('//div[@class="tabModules"]/ul/li/div[@class="module bookSmall"]/a')
end
# Go through every page until there are no more wishlisted books.
page = 0
wishlist = []
begin
books = fetch_books(wishlist_url, page += 1)
books.each do |book|
wishlist << {:title => book.xpath('img/@alt').first.value,
:cover => book.xpath('img/@src').first.value.gsub(/small/, 'medium'),
:title => book.attributes['href'].value}
end
end until books.empty?
pp wishlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment