mizuho_bank
· 🪦 retired
A library for accessing Mizuho Direct (Mizuho Bank’s internet banking) from Ruby. It automates the login page flow (customer number → security questions → password) with Mechanize, letting you pull account balances and transaction statements out as Ruby objects.
It was published as a gem at the time. In an era with no API, it was a scraper for handling your own account data programmatically.
gem install mizuho_bank
Usage looks like this (from the README). It is designed to load credentials from Pit, an account-information manager, rather than hard-coding them.
require 'mizuho_bank'
require 'pit'
pit = Pit.get("MizuhoBank", :require => {
"keiyaku_no" => "keiyaku_no",
"password" => "password",
# 合言葉(秘密の質問)3組の question/answer も同様に登録しておく
})
aikotoba_dict = {
pit['aikotoba1_question'] => pit['aikotoba1_answer'],
pit['aikotoba2_question'] => pit['aikotoba2_answer'],
pit['aikotoba3_question'] => pit['aikotoba3_answer']
}
MizuhoBank.new(pit['keiyaku_no'].to_s, pit['password'].to_s, aikotoba_dict){ |bank|
p bank.info.main_account.money # 口座残高
p bank.info.main_account.cache_flows # 最近の取引履歴
}
Internals
Component layout. The core MizuhoBank class ties together the login handling and page parsing, and results are returned packed into plain data classes: MizuhoDirectInfo / MizuhoAccount / MizuhoCacheFlow.
- Login is a state machine (
lib/mizuho_bank.rb) — rather than hard-coding “which page comes next”,what_is_this_page?determines the page type from phrases found in the current page body (“お客さま番号を入力し…”, “合言葉確認”, etc.), and a loop dispatches to the corresponding input handler. This lets it keep up even when the bank shows or skips authentication steps - Security question (“aikotoba”) support — on the security-question page it extracts the question text with Nokogiri and answers by looking it up in the question-to-answer dictionary (
aikotoba_dict) passed to the constructor. It can handle whichever of the 3 registered questions comes up - Retries — the entire login process is wrapped in retry-handler, a gem of my own, retrying up to 5 times at 5-second intervals on failure
- Out-of-service-hours detection — Mizuho Direct has scheduled maintenance windows; the library scrapes the official out-of-hours notice page, reduces patterns like “late Saturday night every week” and “1st and 4th Saturdays” into a
RepeatDateRange, and refuses to even attempt login if the current time falls within one. English expressions like “1st saturday in this month” are resolved into concrete times with Chronic - Parsing — from the post-login top page it extracts the user name, last login time, the main account (branch name, account type, account number, balance, withdrawable balance) and the 10 most recent transactions; from the statement page it extracts monthly transaction statements. Both use positional table addressing (
nth-childand indices). The site is Shift_JIS, so inputs are converted withtosjisand fetched pages withtoutf8
The flow from login through fetching statements.
As a known issue, the image CAPTCHA demanded after repeated security-question failures is not supported (noted in the README). Also, since the parsing is tightly coupled to the page wording and table structure, this is the kind of code that breaks whenever the site is redesigned.