Skip to content

Instantly share code, notes, and snippets.

@sakjur
Created June 28, 2013 18:24
Show Gist options
  • Select an option

  • Save sakjur/5886867 to your computer and use it in GitHub Desktop.

Select an option

Save sakjur/5886867 to your computer and use it in GitHub Desktop.
Ruby lösfiler från 2011
# Copyright (C) 2011 by
# Emil @sakjur Tullstedt <[email protected]>
#
# This Ruby script requires:
# gem mechanize
# gem nokogiri (provided by mechanize)
# ruby 1.9
# libxslt-ruby
# libxml2-ruby
# Including gems
require "mechanize"
# Define important variables
agent = Mechanize.new
$pagenum = 1
$totalpages = 28
$storenum = 1
# Download each search result page.
while $pagenum <= $totalpages do
page = agent.get('http://www.apotekhjartat.se/hitta-apotek-hjartat/?q=*&defbn=' + $pagenum.to_s)
# Increase counter by one.
$pagenum = $pagenum+1 # CAUTION! Eternal loop if removed.
# Print each store
page.search('.pharmacy-search-result h2 a').each do |content|
# Prints some relevant information
puts $storenum
puts content.text + "\n"
puts content.attribute("href").to_s + "\n"
# Increase counter by one.
$storenum = $storenum+1
end
end
#
# Copyright (C) 2011 by
# Emil @sakjur Tullstedt <[email protected]>
#
# This Ruby script requires:
# gem mechanize
# ruby 1.9
# libxslt-ruby
# libxml2-ruby
require "mechanize"
agent = Mechanize.new
$sidnr = 1
$butiknr = 1
while $sidnr <= 50 do
page = agent.get('http://www.coop.se/Sok/Butikssok/?q=*&bn=' + $sidnr.to_s)
$sidnr = $sidnr+1
page.search('.shopResult').each do |content|
$iv = 0
$ih = 0
$dayname = Array.new
$hours = Array.new
$i = 0
puts $butiknr
puts content.search('dl dt a').text
content.search('ul li').each do |oppettid|
oppettid.text.delete("<li>").delete("</li>")
if oppettid.text.match(/[0-2]?[0-9]\:[0-6][0-9]/) == nil and oppettid.text.match(/stängt|avvikande öppettider|veckans erbjudande|visa på kartan/) == nil
$dayname[$iv] = oppettid.text
$iv += 1
end
if oppettid.text.match(/[0-2]?[0-9]\:[0-6][0-9]/) or oppettid.text.downcase.match(/stängt/)
$hours[$ih] = oppettid.text
$ih += 1
end
end
$dayname.each do |opentheday|
if($hours[$i]) # This row prevents the application from crashing
# THIS IS ONLY A TEST.
puts opentheday + " " + $hours[$i]
$i += 1
end
end
$butiknr = $butiknr+1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment