本文介绍了如何从Mechanize :: File对象转换为Mechanize :: Page对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a page that logs into a form. After logging in there are a few redirects. The first one looks like this:

#<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时,我会得到

so when I do a page.class here I get

Mechanize::File

如何将其转换为Mechanize::Page?

@pguardiario

@pguardiario

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

To better explain I have the code in my original message is stored in page.

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

When I do page.class I get Mechanize::File

因此,我在上面摘录了您的代码:

So then I excute your code above:

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

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

So I do this: agent.get(page.uri.to_s) or event try with any url agent.get("https://www.manageyourloans.com/MYL")I get an error: ArgumentError: wrong number of arguments (4 for 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?

My question is once I do this, how do I convert the previous page into a Mechanize::Page?

推荐答案

您可以通过将文件对象中包含的主体作为对象的主体传递给Mechanize :: File,将其转换为Mechanize :: Page.新页面:

You can convert from a Mechanize::File to a Mechanize::Page by taking the body contained in the file object and passing that in as the body of a new 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中看到它读取的文件的内容.

First, I created a fake Mechanize::File object just to have one for the example code to follow. You can see the content of the file it read in the :body.

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

Mechanize creates a Mechanize::File object when it can't figure out what the true content-type is.

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转换为您应该知道的样子即可.

Just pass in the body of the file object and let Mechanize convert to what you know it should be.

这篇关于如何从Mechanize :: File对象转换为Mechanize :: Page对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:50