Class: ChildrensBooks::Scraper

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

Class Method Summary collapse

Class Method Details

.scrapeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/childrens_books/scraper.rb', line 3

def self.scrape
    html = open("https://www.commonsensemedia.org/lists/50-books-all-kids-should-read-before-theyre-12")
    doc = Nokogiri::HTML(html)
    
    doc.css(".views-row-odd").each do |node|
        title = node.css("strong.field-content a").text
        author = node.css("div.views-field-field-term-book-authors").text.gsub("By", "").strip
        description = node.css("div.views-field-field-one-liner").text.strip
        age = node.css("div.views-field-field-review-recommended-age").text.strip.delete("age").delete("+").split.join("")
        year = node.css("div.views-field-field-canonical-date").text.strip.gsub(/[()]/, "") 
        url = node.css("strong.field-content a").attr("href").value
        ChildrensBooks::Book.new(title, author, description, age, year, url)
    end
    
    doc.css(".views-row-even").each do |node|
        title = node.css("strong.field-content a").text
        author = node.css("div.views-field-field-term-book-authors").text.gsub("By", "").strip
        description = node.css("div.views-field-field-one-liner").text.strip
        age = node.css("div.views-field-field-review-recommended-age").text.strip.delete("age").delete("+").split.join("")
        year = node.css("div.views-field-field-canonical-date").text.strip.gsub(/[()]/, "")
        url = node.css("strong.field-content a").attr("href").value
        ChildrensBooks::Book.new(title, author, description, age, year, url) 
    end
end