Created
January 10, 2011 10:45
-
-
Save hanachin/772629 to your computer and use it in GitHub Desktop.
学内SNSで自分の過去日記のurl一覧をとりだすためのもの。もうちょっと整理したい。
This file contains 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
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'highline/import' | |
require 'mechanize' | |
top_page_url = URI.parse('http://uppi.it-college.ac.jp/') | |
username = ask("Enter your username: "){|q| q.echo = true } | |
password = ask("Enter your password: "){|q| q.echo = '*' } | |
agent = Mechanize.new | |
# login | |
agent.get(top_page_url).form_with(:action => "./"){|form| | |
form.field_with(:name => "username").value = username | |
form.field_with(:name => "password").value = password | |
form.submit | |
} | |
login_result = agent.page | |
abort("login failed.") if login_result.uri.to_s["login_failed"] | |
# get member id | |
agent.page.link_with(:href => /page_h_prof/).click | |
member_id = agent.page.at("/html/body/div/div/div[3]/div/div/div/p[2]").text.to_s[/[0-9]+/].to_i | |
# get diary page urls | |
next_link = agent.page.link_with(:href => /page_fh_diary_list/) | |
begin | |
next_link.click | |
next_link = agent.page.link_with(:text => "次を表示") | |
agent.page.links_with(:href => /target_c_diary_id=[0-9]+$/).each {|link| | |
puts agent.page.uri + link.uri | |
} | |
end while next_link | |
# logout | |
login_result.link_with(:href => /logout/).click | |
logout_result = agent.page | |
abort("logout failed.") unless logout_result.uri.to_s["msg_code=logout"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment