# Hpricot - best way to parse based on comments

**URL:** <https://www.ruby-forum.com/t/hpricot-best-way-to-parse-based-on-comments/76762>\
**Category:** Ruby\
**Created:** [November 20, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/hpricot-best-way-to-parse-based-on-comments/76762 "2006-11-20T23:52:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jerome](https://www.ruby-forum.com/u/jerome)\
**Post date:** [November 20, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/hpricot-best-way-to-parse-based-on-comments/76762/1 "2006-11-20T23:52:09Z")

</div>

I am trying to parse some files that contain comments like this:

images, text, etc…

Interesting text of site here.

I am wondering how to go about extracting the data within the comments  
block using Hpricot. I am not aware of a way to refer to commented HTML  
through CSS or XPath selectors.

Thanks for any ideas!

- Jerome

---

<div class="post-metadata">

**Author:** ![jerome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jerome](https://www.ruby-forum.com/u/jerome)\
**Post date:** [November 21, 2006, 12:50am UTC](https://www.ruby-forum.com/t/hpricot-best-way-to-parse-based-on-comments/76762/2 "2006-11-21T00:50:51Z")

</div>

On 11/20/06, Jerome — [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to parse some files that contain comments like this:  
> …  
> I am not aware of a way to refer to commented HTML  
> through CSS or XPath selectors.

The XPath comment() selector will select all comments:

For example (xpath after -m flag):  
keith@devel ~ $ xml sel -t -m ‘//comment()’ -v ‘.’ -n simple.xml  
one comment  
two comment

keith@devel ~ $ cat simple.xml

HTH,  
Keith

---

<div class="post-metadata">

**Author:** ![jerome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jerome](https://www.ruby-forum.com/u/jerome)\
**Post date:** [November 24, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/hpricot-best-way-to-parse-based-on-comments/76762/3 "2006-11-24T20:54:42Z")

</div>

Jerome — wrote:

> Interesting text of site here.
> 
> I am wondering how to go about extracting the data within the comments  
> block using Hpricot.

The best and easiest way to parse this file using Hpricot with your  
required  
specification … is not to use Hpricot.

start\_mark = “”  
end\_mark = “”

data = File.read(page\_path)

output = data.scan(%r{#{start\_mark}(.\*?)#{end\_mark}}m)

All done, finished, no poring over documentation, no considering  
rewriting  
the library to get it to do what you actually want, done.

By the way. Did I mention that inserting new data into the same page  
structure is about the same level of difficulty?
