Class: Libri::Scraper
- Inherits:
-
Object
- Object
- Libri::Scraper
- Defined in:
- lib/libri/scraper.rb
Instance Attribute Summary collapse
-
#award ⇒ Object
Returns the value of attribute award.
-
#book ⇒ Object
Returns the value of attribute book.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #scrape_award(award) ⇒ Object
- #scrape_barnes_noble ⇒ Object
- #scrape_book(book) ⇒ Object
- #scrape_quote ⇒ Object
Instance Attribute Details
#award ⇒ Object
Returns the value of attribute award.
3 4 5 |
# File 'lib/libri/scraper.rb', line 3 def award @award end |
#book ⇒ Object
Returns the value of attribute book.
3 4 5 |
# File 'lib/libri/scraper.rb', line 3 def book @book end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/libri/scraper.rb', line 3 def url @url end |
Instance Method Details
#scrape_award(award) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/libri/scraper.rb', line 20 def scrape_award(award) html = award[:url] books_page = Nokogiri::HTML(open(html)) books = {} books_array = books_page.css("div.product-shelf-info").take(20).map { |book| books = { :title => book.css("div.product-shelf-title").text.strip, :author => book.css("div.product-shelf-author").text.strip, :url => "https://www.barnesandnoble.com" + book.css("a").attribute("href").value } }.uniq end |
#scrape_barnes_noble ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/libri/scraper.rb', line 5 def html = "https://www.barnesandnoble.com/b/books/awards/_/N-8q8Z1d6q?showMoreIds=10008" awards_page = Nokogiri::HTML(open(html)) awards = {} # Chosen .take(28) because without it, our awards list will include a 'Show Less' awards_array = awards_page.css("ul#sidebar-section-0 li a").take(28).map { |award| awards = { :name => award.text.chomp, :url => "https://www.barnesandnoble.com" + award.attribute("href").value } } end |
#scrape_book(book) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/libri/scraper.rb', line 35 def scrape_book(book) html = book[:url] book_page = Nokogiri::HTML(open(html)) info_section = book_page.css("div.tabpanel") book_info_hash = { :title_by_author => info_section.css("div#productInfoOverview div.mb-m").text, :blurbs_and_plot => info_section.css("div#productInfoOverview p").map(&:text).join("\n").strip, :about_author => info_section.css("div#MeetTheAuthor div.text--medium").text.strip, :excerpt => info_section.xpath("//div[@class='read-an-excerpt']/p[not(@class) and position()<3]").map(&:text).join("\n"), :availability => book_page.css("button#pdp-marketplace-btn").text.chomp, :url => book[:url] }.delete_if { |key, val| val.to_s.strip.empty? } end |
#scrape_quote ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/libri/scraper.rb', line 50 def scrape_quote html = "https://www.goodreads.com/quotes/tag/books" quotes_page = Nokogiri::HTML(open(html)) quote_section = quotes_page.css("div.quote") quote_hash = {} quotes_array = quote_section.map { |quote| quote_hash = { :quote => quote.css("div.quoteText").first.text.scan(/(“.+”)/).join(""), :author => quote.css("div.quoteText a").first.text } } end |