-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_ricardo.rb
34 lines (27 loc) · 967 Bytes
/
scrape_ricardo.rb
1
2
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
29
30
31
32
33
34
require 'open-uri'
require 'nokogiri'
class ScrapeRicardo
def initialize(ingredient)
@ingredient = ingredient
end
def scrape
# chocolate_file = File.join(__dir__, "#{ingredient}.html")
html_doc = url_to_html_doc
recipes = []
html_doc.search('#search-results .item-list ul .item .desc')[0...10].each do |recipe|
name = recipe.search('h2 a')[0].text.delete("\u2028")
preptime = recipe.search('ul li')[0].text.match(/[^0-9]*(?<preptime>[0-9]+.*min)/)
preptime = preptime ? preptime["preptime"] : "Unknown"
rating = recipe.search('a .rating') != "" ? recipe.search('a .rating').text : "no rating yet"
recipes << { name: name, preptime: preptime, rating: rating }
end
return recipes
end
private
def url_to_html_doc
url = "https://www.ricardocuisine.com/en/search/key-words/#{@ingredient}"
html_file = open(url).read
html_doc = Nokogiri::HTML(html_file)
return html_doc
end
end