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
26: def initialize(uri=nil, response=nil, body=nil, code=nil, mech=nil)
27: @encoding = nil
28:
29: method = response.respond_to?(:each_header) ? :each_header : :each
30: response.send(method) do |header,v|
31: next unless v =~ /charset/
32: encoding = v[/charset=([^; ]+)/, 1]
33: @encoding = encoding unless encoding == 'none'
34: end
35:
36: # Force the encoding to be 8BIT so we can perform regular expressions.
37: # We'll set it to the detected encoding later
38: body.force_encoding('ASCII-8BIT') if body && body.respond_to?(:force_encoding)
39:
40: @encoding ||= Util.detect_charset(body)
41:
42: super(uri, response, body, code)
43: @mech ||= mech
44:
45: @encoding = nil if html_body =~ /<meta[^>]*charset[^>]*>/
46:
47: raise Mechanize::ContentTypeError.new(response['content-type']) unless
48: response['content-type'] =~ /^(text\/html)|(application\/xhtml\+xml)/
49: @parser = @links = @forms = @meta = @bases = @frames = @iframes = nil
50: end
Find a single base tag matching criteria. Example:
page.base_with(:href => /foo/).click
# File lib/mechanize/page.rb, line 142
142:
Return a list of all base tags
# File lib/mechanize/page.rb, line 242
242: def bases
243: @bases ||=
244: search('base').map { |node| Base.new(node, @mech, self) }
245: end
Find all base tags matching criteria. Example:
page.bases_with(:href => /foo/).each do |base|
puts base.href
end
# File lib/mechanize/page.rb, line 151
151:
Get the content type
# File lib/mechanize/page.rb, line 92
92: def content_type
93: response['content-type']
94: end
# File lib/mechanize/page.rb, line 72
72: def encoding
73: parser.respond_to?(:encoding) ? parser.encoding : nil
74: end
# File lib/mechanize/page.rb, line 58
58: def encoding=(encoding)
59: @encoding = encoding
60:
61: if @parser
62: parser_encoding = @parser.encoding
63: if (parser_encoding && parser_encoding.downcase) != (encoding && encoding.downcase)
64: # lazy reinitialize the parser with the new encoding
65: @parser = nil
66: end
67: end
68:
69: encoding
70: end
Find a single form matching criteria. Example:
page.form_with(:action => '/post/login.php') do |f|
...
end
# File lib/mechanize/page.rb, line 110
110:
Return a list of all form tags
# File lib/mechanize/page.rb, line 216
216: def forms
217: @forms ||= search('form').map do |html_form|
218: form = Form.new(html_form, @mech, self)
219: form.action ||= @uri.to_s
220: form
221: end
222: 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 119
119:
Find a single frame tag matching criteria. Example:
page.frame_with(:src => /foo/).click
# File lib/mechanize/page.rb, line 158
158:
Return a list of all frame tags
# File lib/mechanize/page.rb, line 249
249: def frames
250: @frames ||=
251: search('frame').map { |node| Frame.new(node, @mech, self) }
252: end
Find all frame tags matching criteria. Example:
page.frames_with(:src => /foo/).each do |frame|
p frame.src
end
# File lib/mechanize/page.rb, line 167
167:
Find a single iframe tag matching criteria. Example:
page.iframe_with(:src => /foo/).click
# File lib/mechanize/page.rb, line 174
174:
Return a list of all iframe tags
# File lib/mechanize/page.rb, line 256
256: def iframes
257: @iframes ||=
258: search('iframe').map { |node| Frame.new(node, @mech, self) }
259: end
Find all iframe tags matching criteria. Example:
page.iframes_with(:src => /foo/).each do |iframe|
p iframe.src
end
# File lib/mechanize/page.rb, line 183
183:
# File lib/mechanize/page.rb, line 268
268: def image_urls
269: @image_urls ||= images.map(&:url).uniq
270: end
Return a list of all img tags
# File lib/mechanize/page.rb, line 263
263: def images
264: @images ||=
265: search('img').map { |node| Image.new(node, self) }
266: end
Return a list of all label tags
# File lib/mechanize/page.rb, line 274
274: def labels
275: @labels ||=
276: search('label').map { |node| Label.new(node, self) }
277: end
# File lib/mechanize/page.rb, line 279
279: def labels_hash
280: unless @labels_hash
281: hash = {}
282: labels.each do |label|
283: hash[label.node['for']] = label if label.for
284: end
285: @labels_hash = hash
286: end
287: return @labels_hash
288: end
Find a single link matching criteria. Example:
page.link_with(:href => /foo/).click
# File lib/mechanize/page.rb, line 126
126:
Return a list of all link and area tags
# File lib/mechanize/page.rb, line 206
206: def links
207: @links ||= %{ a area }.map do |tag|
208: search(tag).map do |node|
209: Link.new(node, @mech, self)
210: end
211: end.flatten
212: end
Find all links matching criteria. Example:
page.links_with(:href => /foo/).each do |link|
puts link.href
end
# File lib/mechanize/page.rb, line 135
135:
Return a list of all meta tags
# File lib/mechanize/page.rb, line 226
226: def meta
227: @meta ||= search('head > meta').map do |node|
228: next unless node['http-equiv'] && node['content']
229: (equiv, content) = node['http-equiv'], node['content']
230: if equiv && equiv.downcase == 'refresh'
231: Meta.parse(content, uri) do |delay, href|
232: node['delay'] = delay
233: node['href'] = href
234: Meta.new(node, @mech, self)
235: end
236: end
237: end.compact
238: end
# File lib/mechanize/page.rb, line 76
76: def parser
77: return @parser if @parser
78:
79: if body && response
80: if mech.html_parser == Nokogiri::HTML
81: @parser = mech.html_parser.parse(html_body, nil, @encoding)
82: else
83: @parser = mech.html_parser.parse(html_body)
84: end
85: end
86:
87: @parser
88: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.