How can one get the Hpricot DOM document from Mechanize?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

I was wondering if there were some way of getting the Hpricot DOM (for
lack of a better term) from a Mechanize page. For example:


agent = WWW:Mechanize.new
page = agent.get(http://www.website.com)

# I am currently doing this
doc = Hpricot(page.body)

# I would like to do this
doc = page.get_hpricot_dom


The idea is that since Mechanize apparently uses Hpricot and it's surely
using it to parse the HTML begotten from the agent.get method, it would be
nice if I didn't have to repeat that work.
Is there a way to get this Hpricot document? ...or am I just totally
wrong about how Mechanize uses Hpricot?
Thank you...
 
L

Lex Williams

perhaps it's only me , but would you please detail what is it you want
to accomplish? maybe , with an example perhaps ?
 
A

Aaron Patterson

I was wondering if there were some way of getting the Hpricot DOM (for
lack of a better term) from a Mechanize page. For example:


agent = WWW:Mechanize.new
page = agent.get(http://www.website.com)

# I am currently doing this
doc = Hpricot(page.body)

# I would like to do this
doc = page.get_hpricot_dom


The idea is that since Mechanize apparently uses Hpricot and it's surely
using it to parse the HTML begotten from the agent.get method, it would be
nice if I didn't have to repeat that work.
Is there a way to get this Hpricot document? ...or am I just totally
wrong about how Mechanize uses Hpricot?

You can get at the Hpricot document by using the "parser" accessor on
WWW::Mechanize::page. Page also responds to "search", "/", and "at",
which just delegate to the Hpricot document.

So you can just do:

(agent.get('http://tenderlovemaking.com')/'tr').each do |tr|
...
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top