# Hpricot and regexp parsing help needed please

**URL:** <https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791>\
**Category:** Rails\
**Created:** [September 29, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791 "2008-09-29T22:08:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 29, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791/1 "2008-09-29T22:08:09Z")

</div>

Hello.

I’m using hpricot for the first time on a project. I need to get some  
url’s from a web site, but I only want certain url’s.

I can grab all of the url’s from the page without a problem, but how can  
I enhance this to select [http://www.goodsite.com](http://www.goodsite.com) vs.  
[http://www.wrongsite.com](http://www.wrongsite.com)?

I’d like to test for the string “goodsite”.

Thanks!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 30, 2008, 12:38am UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791/2 "2008-09-30T00:38:33Z")

</div>

> I’m using hpricot for the first time on a project. I need to get some  
> url’s from a web site, but I only want certain url’s.
> 
> I can grab all of the url’s from the page without a problem, but how  
> can  
> I enhance this to select [http://www.goodsite.com](http://www.goodsite.com) vs.  
> [http://www.wrongsite.com](http://www.wrongsite.com)?
> 
> I’d like to test for the string “goodsite”.

…assuming doc is an hpricot object…

doc.search(“a[@href\*=‘goodsite’]”) do |result|  
…  
end

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 30, 2008, 1:03am UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791/3 "2008-09-30T01:03:35Z")

</div>

Philip H. wrote:

> …assuming doc is an hpricot object…
> 
> doc.search(“a[@href\*=‘goodsite’]”) do |result|  
> …  
> end

Yes, that works to only grab the links that I need. Previously though,  
I had used

(doc/:a).each do |link|

this only gave me the html string.

Can I do this the same way instead of returning

\<a href= "http://…

I only want http:// so that I can use these links.

THANKS!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 30, 2008, 1:08am UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791/4 "2008-09-30T01:08:06Z")

</div>

Actually, I was wrong in my previous post. Sorry!! Both results are  
the same, i.e., I get back the \<a href…

Is there a way for me to have a clean link? I want to insert this into  
a table and then pull up the pages.

Thanks!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 30, 2008, 1:40am UTC](https://www.ruby-forum.com/t/hpricot-and-regexp-parsing-help-needed-please/149791/5 "2008-09-30T01:40:53Z")

</div>

Figured it out.

doc.search(“a[@href\*=‘goodsite’]”) do |result|  
link = results.attributes[‘href’]  
puts link  
end
