Created
January 7, 2012 14:52
-
-
Save dannymcc/1574942 to your computer and use it in GitHub Desktop.
nokogiri and mechanize
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
desc "Import incoming calls" | |
task :fetch_incomingcalls => :environment do | |
# Logs into manage.phoneprovider.co.uk and retrieved list of incoming calls. | |
require 'rubygems' | |
require 'mechanize' | |
require 'logger' | |
# Create a new mechanize object | |
agent = Mechanize.new { |a| a.log = Logger.new(STDERR) } | |
# Load the phoneprovider website | |
page = agent.get("https://manage.phoneprovider.co.uk/login") | |
# Select the first form | |
form = agent.page.forms.first | |
form.username = 'username' | |
form.password = 'password' | |
# Submit the form | |
page = form.submit form.buttons.first | |
# Click on link called Call Logs | |
page = agent.page.link_with(:text => "Call Logs").click | |
# Click on link called Incoming Calls | |
page = agent.page.link_with(:text => "Incoming Calls").click | |
# Prints out table rows | |
# puts ('table > tr') | |
# Print out the body as a test | |
# puts page.body | |
# StackOverflow Snippet | |
Nokogiri::HTML(html).xpath("//table/tbody/tr[not(@class)]").collect do |row| | |
timestamp = row.at("td[1]").text.strip | |
source = row.at("td[2]").text.strip | |
destination = row.at("td[3]").text.strip | |
duration = row.at("td[4]").text.strip | |
{:timestamp => timestamp, :source => source, :destination => destination, :duration => duration} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment