How do I convert from a Mechanize::File object to a Mechanize::Page object?

一世执手 提交于 2019-12-06 13:29:01

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
    >
>

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 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

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

I like @The Tin Man's answer but it might be simpler to force the content type of the response:

agent.post_connect_hooks << lambda {|http| http[:response].content_type = 'text/html'}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!