Class: BookwormBuddy::Scraper
- Inherits:
-
Object
- Object
- BookwormBuddy::Scraper
- Defined in:
- lib/bookworm_buddy/scraper.rb
Constant Summary collapse
- @@categories =
[]
- @@bestsellers =
[]
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
Class Method Summary collapse
- .get_books_by_category(category_number) ⇒ Object
- .get_categories ⇒ Object
- .get_description(book_number) ⇒ Object
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
2 3 4 |
# File 'lib/bookworm_buddy/scraper.rb', line 2 def category @category end |
Class Method Details
.get_books_by_category(category_number) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bookworm_buddy/scraper.rb', line 18 def self.get_books_by_category(category_number) @@bestsellers.clear BookwormBuddy::Book.empty bestseller_url = @@categories[category_number.to_i - 1][:url] doc = Nokogiri::HTML(open(bestseller_url)) #binding.pry doc.css('div.col-lg-8.product-info-listView').each do |book| attributes_hash = {} attributes_hash[:title] = book.css('h3.product-info-title a').text attributes_hash[:author] = book.css('div.product-shelf-author.contributors a').first.text attributes_hash[:price] = book.css("span.current a").first.text attributes_hash[:description_url] = "https://www.barnesandnoble.com#{book.css('h3.product-info-title a').attr('href').value}" attributes_hash[:description] = nil @@bestsellers << attributes_hash end BookwormBuddy::Book.create(@@bestsellers) end |
.get_categories ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bookworm_buddy/scraper.rb', line 6 def self.get_categories doc = Nokogiri::HTML(open("https://www.barnesandnoble.com/b/books/_/N-1fZ29Z8q8?&page=1&showMoreIds=10008")) doc.css("ul#sidebar-section-0 li").collect do |link| category_hash = {} category_hash[:name] = link.text.strip category_hash[:url] = "https://www.barnesandnoble.com#{link.css('a.bread-crumbs__item').attr('href').value}" @@categories << category_hash end @@categories.slice!(-1) BookwormBuddy::Category.create(@@categories) end |
.get_description(book_number) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/bookworm_buddy/scraper.rb', line 36 def self.get_description(book_number) book = BookwormBuddy::Book::ALL[book_number.to_i - 1] description_link = book.description_url doc = Nokogiri::HTML(open(description_link)) description = doc.css("div#Overview div#productInfoOverview p").text book.description = description BookwormBuddy::Book.list_title(book) BookwormBuddy::Book.list_description(book.description) end |