Created
March 5, 2012 04:42
-
-
Save kaosf/1976653 to your computer and use it in GitHub Desktop.
login to Yahoo! JAPAN with Mechanize on Ruby - Ruby の Mechanize を使って Yahoo! JAPAN にログインする
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 'mechanize' | |
Id = 'id' | |
Password = 'password' | |
Mechanize.new do |agent| | |
agent.user_agent = 'Mozilla/5.0 (Linux; U; Android 2.3.2; ja-jp; SonyEricssonSO-01C Build/3.0.D.2.79) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' | |
agent.get 'http://login.yahoo.co.jp/config/login?.lg=jp&.intl=jp&logout=1&.src=www&.done=http://www.yahoo.co.jp' | |
sleep 3 | |
agent.get 'https://login.yahoo.co.jp/config/login?.src=www&.done=http://www.yahoo.co.jp' | |
agent.page.form_with(name: 'login_form') do |form| | |
form.field_with(name: 'login').value = Id | |
form.field_with(name: 'passwd').value = Password | |
agent.page.body =~ /\("\.albatross"\)\[0\]\.value = "(.*)"/ | |
form.field_with(name: '.albatross').value = $1 | |
form.click_button | |
end | |
sleep 3 | |
agent.get 'http://yahoo.co.jp' | |
# ログインに成功してたらログアウトが存在するはず | |
puts true if agent.page.body =~ /ログアウト/ | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
valid on 2012-03-03 (Sat)
2012-03-03 (Sat) 現在有効