Skip to content

Instantly share code, notes, and snippets.

@oquno
Created November 26, 2009 19:01
Show Gist options
  • Save oquno/243609 to your computer and use it in GitHub Desktop.
Save oquno/243609 to your computer and use it in GitHub Desktop.
mixi同級生の人増の増減調査用
#!/usr/local/bin/ruby -Ks
# _*_ coding: utf-8 _*_
require 'rubygems'
require 'mechanize'
require 'time'
MAIL = 'mixi_login_mail_address'
PASS = 'mixi_login_pass'
DIRECTORY = 'log_directory_path'
# start mechanize
def login
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 7'
page = agent.get('http://mixi.jp/')
login_form = page.forms.first
login_form['email'] = MAIL
login_form['password'] = PASS
login_form['next_url'] = '/home.pl'
agent.submit(login_form)
return agent
end
def get_populations(agent)
res = Hash::new
url = 'https://mixi.jp/edit_account_school.pl'
page = agent.get(url)
# tekitou
res['elem'] = page.at('//div[@class="cl01"]/p/a').inner_text.match(/^.+\((\d+)\)$/)[1]
res['junior'] = page.at('//div[@class="cl02"]/p/a').inner_text.match(/^.+\((\d+)\)$/)[1]
res['high'] = page.at('//div[@class="cl03"]/p/a').inner_text.match(/^.+\((\d+)\)$/)[1]
res['univ'] = page.at('//div[@class="cl04"]/p/a').inner_text.match(/^.+\((\d+)\)$/)[1]
return res
end
def save2csv(res)
res.each{|key, value|
content = Time.now.strftime("%Y-%m-%d %H:%M:%S") + ', ' + value
file = DIRECTORY + key + '.csv'
f = File.open(file,'a+')
f.puts content
f.close
}
end
agent = login
save2csv(get_populations(agent))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment