2013-10-28 23 views
0

我使用Ruby與Mechanize GEM登錄到TDAmeritrade的網站。如何使用Ruby Mechanize GEM登錄到TDAmeritrade?

我試圖執行:page = agent.get('https://invest.ameritrade.com/grid/p/login')並如下返回page.forms:

2.0.0-p0 :049 > page.forms
=> [#<Mechanize::Form
{name nil}
{method "POST"}
{action "https://invest.ameritrade.com/grid/p/login"}
{fields
[hidden:0x5b6684a type: hidden name: mAction value: submit]
[hidden:0x5b665a2 type: hidden name: fp_browser value: ]
[hidden:0x5b66386 type: hidden name: fp_screen value: ]
[hidden:0x5b661e2 type: hidden name: fp_software value: ]
[hidden:0x5b6f36e type: hidden name: fp_timezone value: ]
[hidden:0x5b6ffb2 type: hidden name: fp_language value: ]
[hidden:0x5b6fe5e type: hidden name: fp_java value: ]
[hidden:0x5b6fcc4 type: hidden name: fp_cookie value: ]
[hidden:0x5b6fbfc type: hidden name: flashVersion value: ]
[selectlist:0x5b6f756 type: name: ldl value: main:home]}
{radiobuttons}
{checkboxes}
{file_uploads}
{buttons [button:0x5b6eafe type: submit name: value: ]}>
]

正如你可以看到,無論是用戶名,也沒有密碼字段顯示,即使他們可以在瀏覽器的網站上看到。如何使用機械化創業板(我的Ruby腳本)登錄到TDAmeritrade(http://mechanize.rubyforge.org/)?

在此先感謝。

+0

你有沒有試過通常的方式:'form ['username'] ='我的用戶名'? – pguardiario

+0

我試着用下面的URL代替:'http:// www.ameritrade.com'。有了這個,我可以在返回的表單中看到tdUsername和tdPassword。我填寫並提交,但我只收到相同的頁面。它沒有登錄我。 – sizzle

+0

在這種情況下,你是自己的。諸如「我如何登錄到TDAmeritrade」這樣的問題實在太具體,無法在這裏回答。你可能會考慮聘請某人來幫助你。 – pguardiario

回答

0

對於通過POST請求登錄的網站,您可以手動捕獲像Firebug之類的XHR並將其複製到您的代碼中。

例如,您列出的網站在提交用戶名和密碼時會添加一堆隱藏字段。幸運的是,您可以在您的Mechanize :: Agent上將哈希傳遞給POST請求調用。

@ agent.post( 「https://invest.ameritrade.com/grid/p/login」, { 「tbPassword」=> yourPass, 「tbUsername」=> yourUsername})

當然,你需要添加額外的字段好。從螢火蟲

例如POST XHR爲您的示例站點

flashVersion 11.9.900 
fp_browser mozilla/5.0 (macintosh; intel mac os x 10.8; rv:24.0) gecko/20100101 firefox/24.0|5.0 (Macintosh)|MacIntel 
fp_cookie 1 
fp_java 1 
fp_language lang=en-US|syslang=|userlang= 
fp_screen 24|2560|1440|1440 
fp_software 
fp_timezone -4 
ldl main:home 
mAction submit 
tbPassword yourPassword 
tbUsername yourUsername 
+0

感謝您的回覆。我試着將這些字段添加到我的文章中,但仍未登錄。 – sizzle

+0

可能有更多的字段需要比此處更少。返回的頁面是否產生任何錯誤?你可以設置page = @ agent.post和pp。 –

+0

頁面不會產生任何錯誤。這是它返回的內容:http://pastebin.com/raw.php?i=Z6YtX2CM – sizzle

0

您可能會遇到與實現JavaScript的不同部位機械化問題。你可能想嘗試使用watir-webdriver,或者如果它是一個有很多JavaScript使用的網站,你可能會更好使用phantomjs

相關問題