Class: TopTravelDestinations::Scraper

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

Class Method Summary collapse

Class Method Details

.scrape_destination_page(destination_url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/top_travel_destinations/scraper.rb', line 18

def self.scrape_destination_page(destination_url)
    page = Nokogiri::HTML(open(destination_url))
    description_html = page.css("#taplc_expanding_read_more_box_0 .content")
    attractions_html = page.css(".col.attractions li .name")
    weather_high_html = page.css(".temps.wrap span.high")
    weather_low_html = page.css(".temps.wrap span.low")
    flight_price_html = page.css(".flightPrices.wrap .price")

    destination_info = {
        :description => (description_html.text.strip if description_html.text != ""),
        :attractions => (attractions_html.collect {|a| a.text.strip} if attractions_html != []),
        :weather_high => (weather_high_html.text if weather_high_html != ""),
        :weather_low => (weather_low_html.text if weather_low_html != ""),
        :flight_price => (flight_price_html.text.strip.match(/[$][^$]*/) if flight_price_html)       
    }
end

.scrape_main_page(main_page_url) ⇒ Object



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

def self.scrape_main_page(main_page_url)
    page = Nokogiri::HTML(open(main_page_url))

    destinations = page.css(".mainName a").collect do |destination|
         {
            :location => destination.text,
            :destination_url => "https://www.tripadvisor.com#{destination.attribute("href").value}"
        }
    end
    TopTravelDestinations::Destination.create_from_collection(destinations)
end