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 = Mechanize.new agent.get('http://google.com/').class #=> Mechanize::Page
# File lib/mechanize/page.rb, line 26 def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil) @encoding = nil method = response.respond_to?(:each_header) ? :each_header : :each response.send(method) do |header,v| next unless v =~ %rcharset/ encoding = v[%rcharset=([^; ]+)/, 1] @encoding = encoding unless encoding == 'none' end # Force the encoding to be 8BIT so we can perform regular expressions. # We'll set it to the detected encoding later body.force_encoding('ASCII-8BIT') if body && body.respond_to?(:force_encoding) @encoding ||= Util.detect_charset(body) super(uri, response, body, code) @mech ||= mech @encoding = nil if html_body =~ %r<meta[^>]*charset[^>]*>/ raise Mechanize::ContentTypeError.new(response['content-type']) unless response['content-type'] =~ %r^(text\/html)|(application\/xhtml\+xml)/ @parser = @links = @forms = @meta = @bases = @frames = @iframes = nil end
Find a single base tag matching criteria
. Example:
page.base_with(:href => %rfoo/).click
# File lib/mechanize/page.rb, line 152
Return a list of all base tags
# File lib/mechanize/page.rb, line 255 def bases @bases ||= search('base').map { |node| Base.new(node, @mech, self) } end
Find all base tags matching criteria
. Example:
page.bases_with(:href => %rfoo/).each do |base| puts base.href end
# File lib/mechanize/page.rb, line 161
Get the content type
# File lib/mechanize/page.rb, line 102 def content_type response['content-type'] end
# File lib/mechanize/page.rb, line 82 def encoding parser.respond_to?(:encoding) ? parser.encoding : nil end
# File lib/mechanize/page.rb, line 68 def encoding=(encoding) @encoding = encoding if @parser parser_encoding = @parser.encoding if (parser_encoding && parser_encoding.downcase) != (encoding && encoding.downcase) # lazy reinitialize the parser with the new encoding @parser = nil end end encoding end
Find a single form matching criteria
. Example:
page.form_with(:action => '/post/login.php') do |f| ... end
# File lib/mechanize/page.rb, line 120
Return a list of all form tags
# File lib/mechanize/page.rb, line 229 def forms @forms ||= search('form').map do |html_form| form = Form.new(html_form, @mech, self) form.action ||= @uri.to_s form end end
Find all forms form matching criteria
. Example:
page.forms_with(:action => '/post/login.php').each do |f| ... end
# File lib/mechanize/page.rb, line 129
Find a single frame tag matching criteria
. Example:
page.frame_with(:src => %rfoo/).click
# File lib/mechanize/page.rb, line 168
Return a list of all frame tags
# File lib/mechanize/page.rb, line 262 def frames @frames ||= search('frame').map { |node| Frame.new(node, @mech, self) } end
Find all frame tags matching criteria
. Example:
page.frames_with(:src => %rfoo/).each do |frame| p frame.src end
# File lib/mechanize/page.rb, line 177
Find a single iframe tag matching criteria
. Example:
page.iframe_with(:src => %rfoo/).click
# File lib/mechanize/page.rb, line 184
Return a list of all iframe tags
# File lib/mechanize/page.rb, line 269 def iframes @iframes ||= search('iframe').map { |node| Frame.new(node, @mech, self) } end
Find all iframe tags matching criteria
. Example:
page.iframes_with(:src => %rfoo/).each do |iframe| p iframe.src end
# File lib/mechanize/page.rb, line 193
# File lib/mechanize/page.rb, line 281 def image_urls @image_urls ||= images.map(&:url).uniq end
Return a list of all img tags
# File lib/mechanize/page.rb, line 276 def images @images ||= search('img').map { |node| Image.new(node, self) } end
Return a list of all label tags
# File lib/mechanize/page.rb, line 287 def labels @labels ||= search('label').map { |node| Label.new(node, self) } end
# File lib/mechanize/page.rb, line 292 def labels_hash unless @labels_hash hash = {} labels.each do |label| hash[label.node['for']] = label if label.for end @labels_hash = hash end return @labels_hash end
Find a single link matching criteria
. Example:
page.link_with(:href => %rfoo/).click
# File lib/mechanize/page.rb, line 136
Return a list of all link and area tags
# File lib/mechanize/page.rb, line 219 def links @links ||= %w{ a area }.map do |tag| search(tag).map do |node| Link.new(node, @mech, self) end end.flatten end
Find all links matching criteria
. Example:
page.links_with(:href => %rfoo/).each do |link| puts link.href end
# File lib/mechanize/page.rb, line 145
Return a list of all meta tags
# File lib/mechanize/page.rb, line 239 def meta @meta ||= search('head > meta').map do |node| next unless node['http-equiv'] && node['content'] (equiv, content) = node['http-equiv'], node['content'] if equiv && equiv.downcase == 'refresh' Meta.parse(content, uri) do |delay, href| node['delay'] = delay node['href'] = href Meta.new(node, @mech, self) end end end.compact end
# File lib/mechanize/page.rb, line 86 def parser return @parser if @parser if body && response if mech.html_parser == Nokogiri::HTML @parser = mech.html_parser.parse(html_body, nil, @encoding) else @parser = mech.html_parser.parse(html_body) end end @parser end
# File lib/mechanize/page.rb, line 52 def title @title ||= if doc = parser title = if doc.respond_to?(:title) doc.title else doc.search('title').inner_text end if title && !title.empty? title else nil end end end