Skip to content

Instantly share code, notes, and snippets.

@fractalatcarf
Created January 16, 2018 10:52
Show Gist options
  • Save fractalatcarf/934499dec316a650852fc6046bc05119 to your computer and use it in GitHub Desktop.
Save fractalatcarf/934499dec316a650852fc6046bc05119 to your computer and use it in GitHub Desktop.
lecture du cours de scrapping
require 'open-uri'
require 'nokogiri'
require 'csv'
ingredient = 'chocolate'
compter = 0
array = []
10.times do
puts "page #{compter}"
url = "http://www.letscookfrench.com/recipes/find-recipe.aspx?s=#{ingredient}&start=#{compter}"
compter += 10
html_file = open(url).read
html_doc = Nokogiri::HTML(html_file)
html_doc.search('.m_titre_resultat a').each do |element|
array << [element.text.strip, element.attribute('href').value]
end
end
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
filepath = 'chocolate.csv'
CSV.open(filepath, 'wb', csv_options) do |csv|
csv << ['title', 'url']
array.each do |receipe|
csv << receipe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment