-
-
Save jpcaruana/2956906 to your computer and use it in GitHub Desktop.
Extract emails from google account
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
# 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
Gmail uses IMAP, I guess you could have more robust code using it.
Maybe with http://docs.python.org/library/imaplib.html
You can find example code here : http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/