ruby-on-rails - 如何从 Mechanize::File 对象转换为 Mechanize::Page 对象?

标签 ruby-on-rails ruby ruby-on-rails-3 mechanize-ruby

我有一个登录表单的页面。登录后有一些重定向。第一个看起来像这样:

#<Mechanize::File:0x1f4ff23 @filename="MYL.html", @code="200", @response={"cache-control"=>"no-cache=\"set-cookie\"", "content-length"=>"114", "set-cookie"=>"JSESSIONID=GdJnPVnhtN91KZfQPc3QzM1NLCyWDsnyvpGg8LL0Knnz3RgqxLFs!1803804592!-2134626567; path=/; secure, COOKIE_TEST=Aslyn; secure", "x-powered-by"=>"Servlet/2.4 JSP/2.0"}, @body="\r\n<html>\r\n  <head>\r\n    <meta http-equiv=\"refresh\" content=\"0;URL=MYL?Select=OK&StateName=38\">\r\n  </head>\r\n</html>", @uri=#<URI::HTTPS:0x16e1eff URL:https://www.manageyourloans.com/MYL?StateName=global_CALMLandingPage&GUID=D1704621-1994-E076-460A-10B2B682B960>>

所以当我在这里执行 page.class 时,我得到

Mechanize::File

如何将其转换为 Mechanize::Page


@pguardiario

为了更好地解释,我将原始消息中的代码存储在页面中。

当我执行 page.class 时,我得到 Mechanize::File

然后我执行上面的代码:

agent = Mechanize.new
agent.post_connect_hooks << lambda {|http| http[:response].content_type = 'text/html'}

所以我这样做: agent.get(page.uri.to_s) 或事件尝试使用任何 url agent.get("https://www.manageyourloans.com/MYL ") 我收到错误: ArgumentError:参数数量错误(4 为 1)

我什至尝试过这个:

agent = Mechanize.new { |a|
  a.post_connect_hooks << lambda { |_,_,response,_|
    if response.content_type.nil? || response.content_type.empty?
      response.content_type = 'text/html'
    end
  }
}

我的问题是一旦我这样做了,如何将上一页转换为 Mechanize::Page?

最佳答案

您可以通过获取文件对象中包含的正文并将其作为新页面的正文传递来将 Mechanize::File 转换为 Mechanize::Page:

irb(main):001:0> require 'mechanize'
true
irb(main):002:0> file = Mechanize::File.new(URI.parse('http://foo.com'),nil,File.read('foo.html'))
#<Mechanize::File:0x100ef0190
    @full_path = false,
    attr_accessor :body = "<html><body>foo</body></html>\n",
    attr_accessor :code = nil,
    attr_accessor :filename = "index.html",
    attr_accessor :response = {},
    attr_accessor :uri = #<URI::HTTP:0x100ef02d0
        attr_accessor :fragment = nil,
        attr_accessor :host = "foo.com",
        attr_accessor :opaque = nil,
        attr_accessor :password = nil,
        attr_accessor :path = "",
        attr_accessor :port = 80,
        attr_accessor :query = nil,
        attr_accessor :registry = nil,
        attr_accessor :scheme = "http",
        attr_accessor :user = nil,
        attr_reader :parser = nil
    >
>

首先,我创建了一个假的 Mechanize::File 对象,只是为了有一个供示例代码遵循。您可以在 :body 中看到它读取的文件内容。

当 Mechanize 无法确定真正的内容类型是什么时,它会创建一个 Mechanize::File 对象。

irb(main):003:0> page = Mechanize::Page.new(URI.parse('http://foo.com'),nil,file.body)
#<Mechanize::Page:0x100ed5e30
    @full_path = false,
    @meta_content_type = nil,
    attr_accessor :body = "<html><body>foo</body></html>\n",
    attr_accessor :code = nil,
    attr_accessor :encoding = nil,
    attr_accessor :filename = "index.html",
    attr_accessor :mech = nil,
    attr_accessor :response = {
        "content-type" => "text/html"
    },
    attr_accessor :uri = #<URI::HTTP:0x100ed5ed0
        attr_accessor :fragment = nil,
        attr_accessor :host = "foo.com",
        attr_accessor :opaque = nil,
        attr_accessor :password = nil,
        attr_accessor :path = "",
        attr_accessor :port = 80,
        attr_accessor :query = nil,
        attr_accessor :registry = nil,
        attr_accessor :scheme = "http",
        attr_accessor :user = nil,
        attr_reader :parser = nil
    >,
    attr_reader :bases = nil,
    attr_reader :encodings = [
        [0] nil,
        [1] "US-ASCII"
    ],
    attr_reader :forms = nil,
    attr_reader :frames = nil,
    attr_reader :iframes = nil,
    attr_reader :labels = nil,
    attr_reader :labels_hash = nil,
    attr_reader :links = nil,
    attr_reader :meta_refresh = nil,
    attr_reader :parser = nil,
    attr_reader :title = nil
>
irb(main):004:0> page.class
Mechanize::Page < Mechanize::File

只需传入文件对象的主体,然后让 Mechanize 转换为您知道的内容即可。

关于ruby-on-rails - 如何从 Mechanize::File 对象转换为 Mechanize::Page 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10286700/

相关文章:

ruby-on-rails - ruby gem devise 无法生成初始化程序

ruby-on-rails - 每个用户的rails缓存

javascript - 铁路之路 : Send client data to controller using turbolinks

mysql - MySql 和 Rails 中的时区

ruby-on-rails - Rails Turbo-Stream 更新我的 div 不起作用

ruby-on-rails - rails 上的 ruby 。未初始化常量

c++ - ruby 程序 : Trailing zeros in the nth Power of a numbers factorial

javascript - 莫里斯图不接受我的 json 数据,但接受硬编码的示例数据

ruby-on-rails - 在语义/循环/形式中访问对象

ruby-on-rails - 用于 hostingrails 的 sendmail_setting