2016-05-17 43 views
0

我想從本網站獲取我的駕駛執照號碼,issue_date和expiry_date(「https://sarathi.nic.in:8443/nrportal/sarathi/HomePage.jsp」)。當我嘗試獲取它時,出現錯誤Mechanize::ResponseCodeError: 500 => Net::HTTPInternalServerError for https://sarathi.nic.in:8443/nrportal/sarathi/DlDetRequest.jsp -- unhandled response如何解決HTTP 500錯誤,同時使用Ruby進行機械化報廢?

這是我寫的刮代碼:

require 'mechanize' 
require 'logger' 
require 'nokogiri' 
require 'open-uri' 
require 'openssl' 

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 
agent = Mechanize.new 
agent.log = Logger.new "mech.log" 
agent.user_agent_alias = 'Mac Safari 4' 
Mechanize.new.get("https://sarathi.nic.in:8443/nrportal/sarathi/HomePage.jsp") 

page=agent.get('https://sarathi.nic.in:8443/nrportal/sarathi/HomePage.jsp') # opening home page. 
page = agent.page.links.find { |l| l.text == 'Status of Licence' }.click   # click the link. 
page.forms_with(:name=>"dlform").first.field_with(:name=>"dlform:DLNumber").value="TN3‌​8 20120001119" #user input to text field. 
page.form_with(:name=>"dlform").field_with(:name=>"javax.faces.ViewState").value="SUBMIT" #submit button value assigning. 
page.form(:name=>"dlform",:action=>"/nrportal/sarathi/DlDetRequest.jsp") #to specify the form i need. 
agent.cookie_jar.clear! 
gg=agent.submit page.forms.last #submitting my form 

回答

0

這是行不通的,因爲你是在提交表單前清償的餅乾,因此刪除您所提供的所有輸入數據。我能得到它的工作通過刪除其簡稱爲:

... 

page.forms_with(:name=>"dlform").first.field_with(:name=>"dlform:DLNumber").value="TN3‌​8 20120001119" #user input to text field 

form = page.form(:name=>"dlform",:action=>"/nrportal/sarathi/DlDetRequest.jsp") 
gg = agent.submit form, form.buttons.first 

請注意,您不需要設置#submit按鈕的值,而通過提交按鈕,而提交表單本身。

+0

謝謝先生,但有一件事!我如何從輸出中提取我的DL號,發行日期和到期日期? –

+0

你可以使用存儲「gg」的提交表單的響應來找到它。儘管您提供的號碼對我來說不起作用,並且從UI中嘗試失敗,所以只需檢查一次即可。 – Sam

+0

噢,謝謝,我知道了,先生,再次感謝................................ –