# Newbie to Ruby

**URL:** <https://rubytalk.org/t/newbie-to-ruby/40208>\
**Category:** ruby-talk\
**Created:** [27 August 2007 03:29 UTC](https://rubytalk.org/t/newbie-to-ruby/40208 "2007-08-27T03:29:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Pareto](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Charles\_Pareto](https://rubytalk.org/u/Charles_Pareto)\
**Post date:** [27 August 2007 03:29 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/1 "2007-08-27T03:29:07Z")

</div>

Hi,  
I was reading through Learning Ruby and was trying to get the example on  
page 119 to work to scrape Google. But when I run it nothing happens.  
Any help would be appreciated. Thanks.

require 'open-uri'

url = "[http://www.google.com/search?q=ruby](http://www.google.com/search?q=ruby)"

open(url) { |page| page\_content = page.read()

links = page\_content.scan(/\<a class=1.\*?href=\"(.\*?)\"/).flatten

links.each {|link| puts link}

}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mark\_Gallop1](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mark\_Gallop1](https://rubytalk.org/u/Mark_Gallop1)\
**Post date:** [27 August 2007 04:01 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/2 "2007-08-27T04:01:10Z")

</div>

Hi Charles,

Charles Pareto wrote:

> links = page\_content.scan(/\<a class=1.\*?href=\"(.\*?)\"/).flatten  
> &nbsp;&nbsp;

I don't think that regular expression (regexp) works. Maybe google has changed their code since the book was written. I think it goes "href" then "class".

If you work out the correct regexp, let us know.

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![Dan\_Zwell](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@Dan\_Zwell](https://rubytalk.org/u/Dan_Zwell)\
**Post date:** [27 August 2007 04:26 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/3 "2007-08-27T04:26:01Z")

</div>

Mark Gallop wrote:

> Hi Charles,
> 
> Charles Pareto wrote:
> 
> > links = page\_content.scan(/\<a class=1.\*?href=\"(.\*?)\"/).flatten  
> > &nbsp;&nbsp;
> 
> I don't think that regular expression (regexp) works. Maybe google has changed their code since the book was written. I think it goes "href" then "class".
> 
> If you work out the correct regexp, let us know.
> 
> Cheers,  
> Mark

As Mark said, google changed their code somewhat. If you work out the correct regular expression and it still seems to give erratic results, here is a hint: the naive solution uses ".\*?" in a certain place, but that will still match too many results. Try [^"]\*? instead, because you probably don't want to match quotes. (I just tried this, and that was the problem I encountered.)

By the way, a robust regex to match all HTML links looks kind of nasty, but perhaps you should try writing one--it's a good exercise. (Of course, that's not what you want for this--you want to match all links of class=l.)

Regards,  
Dan

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [27 August 2007 05:13 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/4 "2007-08-27T05:13:11Z")

</div>

As those guys said, Google probably changed their code since the book was written.  
That's not to prevent web scraping, it's just that web sites are pretty transitory. They change all the time and very easily. This makes web sophisticated web scraping a moving target.

---

<div class="post-metadata">

**Author:** ![Jaime\_Iniesta](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Jaime\_Iniesta](https://rubytalk.org/u/Jaime_Iniesta)\
**Post date:** [27 August 2007 18:22 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/5 "2007-08-27T18:22:12Z")

</div>

Yes, web scraping using just open-uri and regular expressions is  
pretty low-level.

Try Hpricot or scRUBYt for a higher level, more flexible, scraping.¡

> **···**
>
> 2007/8/27, John Joyce \<dangerwillrobinsondanger@gmail.com\>:
> 
> > That's not to prevent web scraping, it's just that web sites are  
> > pretty transitory. They change all the time and very easily. This  
> > makes web sophisticated web scraping a moving target.
> 
> --  
> Jaime Iniesta  
> [http://jaimeiniesta.com](http://jaimeiniesta.com) - [http://railes.net](http://railes.net) - [http://freelancegirona.com](http://freelancegirona.com)

---

<div class="post-metadata">

**Author:** ![Charles\_Pareto](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Charles\_Pareto](https://rubytalk.org/u/Charles_Pareto)\
**Post date:** [29 August 2007 22:15 UTC](https://rubytalk.org/t/newbie-to-ruby/40208/6 "2007-08-29T22:15:23Z")

</div>

Jaime Iniesta wrote:

> **···**
>
> > 2007/8/27, John Joyce \<dangerwillrobinsondanger@gmail.com\>:
> > 
> > > That's not to prevent web scraping, it's just that web sites are  
> > > pretty transitory. They change all the time and very easily. This  
> > > makes web sophisticated web scraping a moving target.
> > 
> > Yes, web scraping using just open-uri and regular expressions is  
> > pretty low-level.
> > 
> > Try Hpricot or scRUBYt for a higher level, more flexible, scraping.�
> 
> So I was trying out what everyone said and I got it to work. Here is  
> what I did.
> 
> page\_content.scan(/\<a href=\"([^"]\*?)\" class=l[^"]\*?/).flatten  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
