Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My SoftBank通信量確認画面の情報をスクレイピングしてメール通知するスクリプト
require "mechanize"
require "yaml"
# 設定ファイル
config = YAML.load_file("config.yml")
# スクレイピング準備
agent = Mechanize.new
agent.user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36"
agent.max_history = 1
# 通信速度状況の確認・変更ページ
agent.get("https://my.softbank.jp/msb/d/webLink/doSend/MRERE0000")
# ログインフォーム送信
agent.page.form_with(:name => 'loginForm') {|f|
f.field_with(:id => 'id').value = config["login_id"]
f.field_with(:id => 'password').value = config["password"]
f.click_button
}
# 転送画面
action = agent.page.at('form[@name="webLink"]').attributes["action"].value
mfiv = agent.page.at('input[@name="mfiv"]').attributes["value"].value
mfsb = agent.page.at('input[@name="mfsb"]').attributes["value"].value
agent.post(action, {mfiv: mfiv, mfsb: mfsb})
# データ取得
lines = []
contract_info = agent.page.search('table[@class="contract-info"] tr')
contract_info.each do |info|
th = info.children[0].inner_text.strip
td = info.children[2].inner_text.strip
lines << th + ': ' + td
end
lines << ''
contract_info = agent.page.search('form[@name="customerTrafficStateActionForm"] table tr')
contract_info.each do |info|
th = info.children[0].inner_text.strip
td = info.children[2].inner_text.strip
lines << th + ': ' + td
end
# メール送信
mail_body = lines.join("\n")
# メール送信
`echo "#{mail_body}" | /bin/mail -s "SoftBank 通信速度状況" -r [src_mail_addess] [dest_mail_address]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment