Skip to content

Instantly share code, notes, and snippets.

@conorgriffin
Last active August 29, 2015 14:08
Show Gist options
  • Save conorgriffin/74f5ab96b5c1e91a7571 to your computer and use it in GitHub Desktop.
Save conorgriffin/74f5ab96b5c1e91a7571 to your computer and use it in GitHub Desktop.
Scraping a web page with Nokogiri and mailing the results with ActionMailer
require 'nokogiri'
require 'open-uri'
require_relative 'SimpleMailer'
doc = Nokogiri::HTML(open("http://www.camerapricebuster.co.uk/Canon/Canon-Lenses"))
# This xpath gathers the relevant table rows into an array. The xpath was worked
# out manually by using Developer Tools in Chrome and mapping the page structure into
# an xpath query. It looks for table rows containing both the 'img' element with the
# correct source attribute and an 'a' element with 'Canon-EF-lenses' in the href
# attribute.
elements = doc.xpath("//tr[td/a/img[@src='/i/ple1.gif'] and td/a[contains(@href,'Canon-EF-lenses')]]")
body = "\nFound #{elements.length} item#{elements.length < 1 || elements.length > 1 ? 's' : ''} "\
"with lowest-ever price#{elements.length < 1 || elements.length > 1 ? 's' : ''}\n\n"
elements.each {
|item|
lens = item.xpath("td[2]/a/@href")
price = item.xpath("td[2]/a/text()")
url = "http://www.camerapricebuster.co.uk#{lens}"
price = price.to_s.gsub("&pound;", "£")
lens = lens.to_s.gsub("/Canon/Canon-EF-lenses/", "").gsub("-", " ")
body += "Lens:\t#{lens}\nPrice:\t#{price}\nURL:\t#{url}\n\n\n"
}
email = SimpleMailer.simple_message('recipient@gmail.com', 'Camera Prices', body)
puts body
email.deliver
require 'action_mailer'
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'sender@gmail.com',
:password => 'senders-gmail-app-password',
:authentication => :plain,
}
class SimpleMailer < ActionMailer::Base
def simple_message(recipient, subject, message)
mail(:from => 'sender@gmail.com',
:to => recipient,
:subject => subject,
:body => message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment