Class: NYTFiction::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/nyt_fiction/scraper.rb

Class Method Summary collapse

Class Method Details

.scrape_booksObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nyt_fiction/scraper.rb', line 3

def self.scrape_books
  url = open("http://www.nytimes.com/books/best-sellers/combined-print-and-e-book-fiction", :allow_redirections => :all)
  page = Nokogiri::HTML(url.read)
  page.encoding = 'utf-8'
  page.css(".book-body").each do |b|
    book = NYTFiction::Book.new
    book.title = b.css("h2.title").text.split.map {|w| w.capitalize}.join(" ")
    book.author = b.css(".author").text
    book.publisher = b.css(".publisher").text
    book.description = b.css(".description").text.strip
    book.freshness = b.css(".freshness").text
    book.save
  end
end