Class: BlueCrossPets::Scraper
- Inherits:
-
Object
- Object
- BlueCrossPets::Scraper
- Defined in:
- lib/blue_cross_pets/scraper.rb
Class Method Summary collapse
Class Method Details
.scrape_index(index_url) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/blue_cross_pets/scraper.rb', line 3 def self.scrape_index(index_url) html = open(index_url) pet_index = Nokogiri::HTML(html) pets = [] profile_url = pet_index.css("a.item__link").each do |pet_info| name = pet_info.css("h3.item__title").text breed = pet_info.css("ul.item__body li")[0].text gender = pet_info.css("ul.item__body li")[1].text age = pet_info.css("ul.item__body li")[2].text.gsub("\n", "").gsub("\t", "").strip profile_url = "https://www.bluecross.org.uk" + pet_info.attribute("href").value if pet_info.css("div.banner").length > 0 availability = "Reserved" elsif pet_info.css("div.banner").length == 0 availability = "Available" end pets << {name: name, breed: breed, gender: gender, age: age, profile_url: profile_url, availability: availability} end pets end |
.scrape_profile(profile_url) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/blue_cross_pets/scraper.rb', line 30 def self.scrape_profile(profile_url) pet_profile = Nokogiri::HTML(open(profile_url)) attributes_hash = {} if pet_profile.css("div.column-main p").length > 0 attributes_hash[:bio] = pet_profile.css("div.column-main p").text end pet_profile.css("div.column-aside").each do |attribute| attributes_hash[:breed_and_colour] = attribute.css("li.pet-details_species").text.split(" - ")[1].strip attributes_hash[:reference] = attribute.css("li.pet-details_reference").text.gsub("\n", "").split(":")[1].strip if attribute.css("li.pet-details_info").length > 0 attributes_hash[:can_live_with] = attribute.css("li.pet-details_info").text.gsub("\n", "").split(":")[1].strip end end attributes_hash end |