This class encapsulates an HTML page. If Mechanize finds a content type of ‘text/html’, this class will be instantiated and returned.
require 'rubygems' require 'mechanize' agent = WWW::Mechanize.new agent.get('http://google.com/').class #=> WWW::Mechanize::Page
pretty_inspect | -> | inspect |
mech | [RW] |
# File lib/www/mechanize/page.rb, line 29 29: def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil) 30: super(uri, response, body, code) 31: @mech ||= mech 32: 33: raise Mechanize::ContentTypeError.new(response['content-type']) unless 34: content_type() =~ /^text\/html/ 35: 36: @parser = @links = @forms = @meta = @bases = @frames = @iframes = nil 37: end
# File lib/www/mechanize/page.rb, line 102 102: def bases 103: @bases ||= WWW::Mechanize::List.new( 104: search('base').map { |node| Base.new(node, @mech, self) } 105: ) 106: end
Get the content type
# File lib/www/mechanize/page.rb, line 51 51: def content_type 52: response['content-type'] 53: end
# File lib/www/mechanize/page.rb, line 77 77: def forms 78: @forms ||= WWW::Mechanize::List.new( 79: search('form').map do |html_form| 80: form = Form.new(html_form, @mech, self) 81: form.action ||= @uri 82: form 83: end 84: ) 85: end
# File lib/www/mechanize/page.rb, line 108 108: def frames 109: @frames ||= WWW::Mechanize::List.new( 110: search('frame').map { |node| Frame.new(node, @mech, self) } 111: ) 112: end
# File lib/www/mechanize/page.rb, line 114 114: def iframes 115: @iframes ||= WWW::Mechanize::List.new( 116: search('iframe').map { |node| Frame.new(node, @mech, self) } 117: ) 118: end
# File lib/www/mechanize/page.rb, line 67 67: def links 68: @links ||= WWW::Mechanize::List.new( 69: %w{ a area }.map do |tag| 70: search(tag).map do |node| 71: Link.new(node, @mech, self) 72: end 73: end.flatten 74: ) 75: end
# File lib/www/mechanize/page.rb, line 87 87: def meta 88: @meta ||= WWW::Mechanize::List.new( 89: search('meta').map do |node| 90: next unless node['http-equiv'] && node['content'] 91: (equiv, content) = node['http-equiv'], node['content'] 92: if equiv && equiv.downcase == 'refresh' 93: if content && content =~ /^\d+\s*;\s*url\s*=\s*'?([^\s']+)/i 94: node['href'] = $1 95: Meta.new(node, @mech, self) 96: end 97: end 98: end.compact 99: ) 100: end