Class: RestaurantScrapper

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

Class Method Summary collapse

Class Method Details

.scrape(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/restaurantscrapper.rb', line 6

def self.scrape(value)
  
outputArray = []

args = %w[--disable-infobars --headless window-size=1600,1200 --no-sandbox --disable-gpu --disable-dev-shm-usage]
options = {
       binary: ENV['GOOGLE_CHROME_BIN'],
       prefs: { password_manager_enable: false, credentials_enable_service: false },
       args:  args
     }

browser = Watir::Browser.new(:chrome, options: options)
browser.goto value
doc = Nokogiri::HTML.parse(browser.html)

#taking all the hotels list in HTML
restaurant_list_snippets = doc.css('div.restaurants-list-ListCell__cellContainer--2mpJS')

#iterating over each hotel
restaurant_list_snippets.each do |restaurant_elements|

	restaurantname_element =	restaurant_elements.search('div.restaurants-list-ListCell__nameBlock--1hL7F')
	restaurant_name = restaurantname_element.search('a.restaurants-list-ListCell__restaurantName--2aSdo').text
	
	type_element1 = restaurant_elements.search('div.restaurants-list-ListCell__infoRow--31xBt.restaurants-list-ListCell__hideLeftDivider--3vXbe.restaurants-list-ListCell__cuisinePriceMenu--r4-Re')
	type_element2 = type_element1.search('span.restaurants-list-ListCell__infoCell--1Fz8a')

	restaurant_type = type_element2.search('span.restaurants-list-ListCell__infoRowElement--2E6E3').text
		
	if restaurant_name
	
	restaurant_name=restaurant_name.split(". ", 2)
	name = restaurant_name[1]
	
	end
	
	if restaurant_type
	restaurant_type=restaurant_type.gsub("\u20AC","money")
	restaurant_type=restaurant_type.split("money", 2)
	restaurant_type[1]= "money#{restaurant_type[1]}"
	restaurant_range=restaurant_type[1].split("-")
	if(restaurant_range.size>1)
	fromrange=restaurant_range[0].scan(/(?=money)/).count
	torange= restaurant_range[1].scan(/(?=money)/).count
	range="#{fromrange}-#{torange}"
	else
	range =restaurant_range[0].scan(/(?=money)/).count
	range="#{range}"
	end
	end
	
	#new Hash is created and all the values are pushed into the hash map
	output = Hash.new
	output.store("name", name)
	output.store("type", restaurant_type[0])
	output.store("range", range)


#pushing to array
	outputArray.push(output)
	
   end
  #returning array
browser.quit()
browser= nil
doc=nil
return outputArray

end