大学の Web サイトから結果を取得するスクリプトを作成しようとしています。誰かが Mechanize を使用することを提案しましたが、それは本当に有望に見えます。
結果を取得するには、最初にロール番号を入力してからセッションを選択する必要があります。最初の部分のシミュレーションは Mechanize で簡単にできましたが、2 番目の部分は実際には JavaScriptonchange
イベントであるため、問題が発生しています。
JavaScript の関数定義を読みましたが、これがこれまでに思いついたものです。Mechanize は onchange イベントを処理できず、実際に JavaScript 関数によって変更された値を手動で渡すと、同じページが返されます。
これがJavaScriptコードです
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
firebug にブレークポイントを設定したところ、値__EVENTTARGET
が「Dt1」であることがわかりましたが、 __EVENTARGUMENT
「」のままです。
これを行うために私が書いたルビースクリプトは
require 'mechanize'
#set up the agent to mimic firefox on windows
agent = Mechanize.new
agent.keep_alive = true
agent.user_agent = 'Windows Mozilla'
page = agent.get('http://www.nitt.edu/prm/nitreg/ShowRes.aspx')
#using mechanize to get us past the first form presented
result_form = page.form('Form1')
result_form.TextBox1 = '205110018'
page = agent.submit( result_form, result_form.buttons.first )
#the second hurdle that we encounter,
#here i'm trying to get past the JavaScript by doing what it does manually
result_form = page.form('Form1')
result_form.field_with('Dt1').options.find { |opt| opt.value == '66' }.select
result_form.field_with( :name => '__EVENTTARGET' ).value = 'Dt1'
#here i should have got the page with the results
page = agent.submit(result_form)
pp page
誰が私が間違っているのか教えてもらえますか?