gpt4 book ai didi

ruby - 在 ruby​​ 中使用 Mechanize 进行网页抓取时如何解决 HTTP500 错误?

转载 作者:行者123 更新时间:2023-12-04 16:21:28 37 4
gpt4 key购买 nike

我想从这个网站(“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 .

这是我编写的用于抓取的代码:

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

最佳答案

它不起作用,因为您在提交表单之前清除了 cookie,因此删除了您提供的所有输入数据。我可以通过简单地将其删除来使其工作:

...

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 按钮设置值,而是在表单提交本身时传递提交按钮。

关于ruby - 在 ruby​​ 中使用 Mechanize 进行网页抓取时如何解决 HTTP500 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37268013/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com