# How can one get the Hpricot DOM document from Mechanize?

**URL:** <https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507>\
**Category:** Ruby\
**Created:** [September 13, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507 "2008-09-13T21:10:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [September 13, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507/1 "2008-09-13T21:10:57Z")

</div>

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](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…

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [September 13, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507/2 "2008-09-13T22:12:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [September 13, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507/3 "2008-09-13T22:23:09Z")

</div>

Just Another Victim wrote:

> # I would like to do this
> 
> doc = page.get\_hpricot\_dom

Try page.parser or page.root (they’re eqivalent).

Regards,  
Matthias

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [September 18, 2008, 7:13am UTC](https://www.ruby-forum.com/t/how-can-one-get-the-hpricot-dom-document-from-mechanize/148507/4 "2008-09-18T07:13:31Z")

</div>

On Sun, Sep 14, 2008 at 04:03:04AM +0900, Just Another Victim of the  
Ambient M. wrote:

> # 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](http://tenderlovemaking.com)’)/‘tr’).each do |tr|  
…  
end
