Created
January 16, 2018 10:52
-
-
Save fractalatcarf/934499dec316a650852fc6046bc05119 to your computer and use it in GitHub Desktop.
lecture du cours de scrapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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