Skip to content

Instantly share code, notes, and snippets.

@andhapp
Created March 28, 2012 23:07
Show Gist options
  • Save andhapp/2231347 to your computer and use it in GitHub Desktop.
Save andhapp/2231347 to your computer and use it in GitHub Desktop.
Extract emails from google account
# Gemfile
source "http://rubygems.org"
gem 'mechanize'
gem 'hpricot'
# Ruby code
require 'bundler'
Bundler.require
END_POINT = 'http://www.gmail.com'
REGEX_TO_MATCH = /^Text you are looking for in the text field/
agent = Mechanize.new
page = agent.get(END_POINT)
form = page.forms.first
puts "Please enter your email:"
form.Email = gets
puts "Please enter your password:"
form.Passwd = gets
puts "Processing..."
puts "-----------------------------------------\n\n"
page = agent.submit form
gmail_page = nil
page.links.each do |link|
text = link.text.strip
if REGEX_TO_MATCH.match(text)
gmail_page = link.click
end
next unless text.length > 0
end
node_set = gmail_page.search("font").find_all { |node| node['color'] == "#7777CC" }
1.upto(5).each do |index|
gmail_page.links.each do |link|
text = link.text.strip
if /^Older/.match(text)
gmail_page = link.click
node_set.concat(gmail_page.search("font").find_all { |node| node['color'] == "#7777CC" })
else
next
end
end
end
node_set.each do |node|
puts node.inner_html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment