2011-09-12 42 views
1

我幾乎沒有紅寶石語言的經驗,我知道它是一種專門用於網絡編程的強大語言。我的問題是如何編寫能夠自動登錄網站並下載每日新聞的程序。即登錄到論壇網站並下載所有線程。 Thnx如何編寫登錄到網站的紅寶石程序

+1

使用[Mechanize](https://github.com/tenderlove/mechanize)。 –

回答

6

對於像這些模擬Web瀏覽器體驗的任務,我使用了mechanize gem。它的工作原理是這樣的:

require 'rubygems' 
require 'mechanize' 

www = Mechanize.new 
www.get('http://your.site/path/to/login/page') do |login_page| 
    inside_page = login_page.form_with(:action => '/path/to/login/form/action') do |f| 
     f.form_username_element_name = "username" 
     f.form_password_element_name = "password" 
    end.click_button 

    # Do stuff with "inside_page", like navigate, scrape links, etc... 
    # See the mechanize docs for details 
end