Last active
August 29, 2015 14:08
-
-
Save conorgriffin/74f5ab96b5c1e91a7571 to your computer and use it in GitHub Desktop.
Scraping a web page with Nokogiri and mailing the results with ActionMailer
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 '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("£", "£") | |
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('[email protected]', 'Camera Prices', body) | |
puts body | |
email.deliver |
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 'action_mailer' | |
ActionMailer::Base.smtp_settings = { | |
:address => 'smtp.gmail.com', | |
:port => 587, | |
:domain => 'gmail.com', | |
:user_name => '[email protected]', | |
:password => 'senders-gmail-app-password', | |
:authentication => :plain, | |
} | |
class SimpleMailer < ActionMailer::Base | |
def simple_message(recipient, subject, message) | |
mail(:from => '[email protected]', | |
: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