Created
September 20, 2015 04:17
-
-
Save vinhnglx/4eaeb2e8511dd1454f42 to your computer and use it in GitHub Desktop.
example_nokogiri_scraping.rb
This file contains hidden or 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' | |
# Store URL to be scraped | |
url = "https://deliveroo.co.uk/restaurants/london/maida-vale?postcode=W92DE&time=1800&day=today" | |
# Parse the page with Nokogiri | |
page = Nokogiri::HTML(open(url)) | |
# Display output onto the screen | |
name =[] | |
page.css('span.list-item-title.restaurant-name').each do |line| | |
name << line.text.strip | |
end | |
category = [] | |
page.css('span.restaurant-detail.detail-cat').each do |line| | |
category << line.text.strip | |
end | |
delivery_time = [] | |
page.css('span.restaurant-detail.detail-time').each do |line| | |
delivery_time << line.text.strip | |
end | |
distance = [] | |
page.css('span.restaurant-detail.detail-distance').each do |line| | |
distance << line.text.strip | |
end | |
status = [] | |
page.css('li.restaurant--details').each do |line| | |
if line.attr("class").include? "unavailable" | |
sts = "closed" | |
else | |
sts = "open" | |
end | |
status << sts | |
end | |
# Write data to CSV file | |
CSV.open("deliveroo.csv", "w") do |file| | |
file << ["Name", "Category", "Delivery Time", "Distance", "Status"] | |
name.length.times do |i| | |
file << [name[i], category[i], delivery_time[i], distance[i], status[i]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment