# Need help with Hpricot

**URL:** <https://www.ruby-forum.com/t/need-help-with-hpricot/150464>\
**Category:** Ruby\
**Created:** [October 8, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/need-help-with-hpricot/150464 "2008-10-08T21:55:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 8, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/need-help-with-hpricot/150464/1 "2008-10-08T21:55:27Z")

</div>

Hi all,

I try to get"Slang " and “A close companion or comrade.” ONLY out of  
the following a webpage(part of it) with hpricot. There are so many  
javascripts there. I don’t think I know path/tag for target.

Thanks,

Li

**sideÃ‚Â·kick** &nbsp; ![](http://cache.lexico.com/g/d/speaker.gif) &nbsp; &nbsp;&nbsp;(sÃ„Â«d'kÃ„Â­k') &nbsp;Pronunciation Key&nbsp;  

n.&nbsp;&nbsp;

_Slang_

A close companion or comrade.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/need-help-with-hpricot/150464/2 "2008-10-09T20:56:08Z")

</div>

On Oct 8, 3:53 pm, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I try to get"Slang " and “A close companion or comrade.” ONLY out of  
> the following a webpage(part of it) with hpricot. There are so many  
> javascripts there. I don’t think I know path/tag for target.

There’s not a whole lot of HTML structure there. If you can  
definitively target the with Hpricot, you can use regular  
expressions to find the appropriate comments and grab the following  
text.

You can get a little more specific with XPath expressions. The  
following sample code (requires libxml-ruby) extracts the two values  
from your sample code:

require ‘xml’  
html = %Q(your\_html\_here)  
doc = XML::HTMLParser.string(html).parse  
puts doc.find(‘//comment()[contains(.,“SUBHEAD”)]/following::i/  
text()’).first  
puts doc.find(‘//comment()[contains(.,“BOF\_DEF”)]/  
following::text()’).first

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/need-help-with-hpricot/150464/3 "2008-10-09T21:33:20Z")

</div>

Hi Mark T.:

Thank you for the suggestion.

I aslo search the forum and find an earlier post which helps me get the  
job done. The ideas of it are 1) use regular expression to remove  
non-convention HMLT stuff such as javascripts. 2) then let hpricot  
handle the remaining. It works pretty good for me.

Here is the title and author of that post/reply:

Re: HTML parser Hpricot? and how to get all text  
Posted by SpringFlowers AutumnMoon (winterheat) on 03.11.2007 09:10

Li
