Search code examples
ruby-on-railsrubyscreen-scrapingmechanizehpricot

How to find the href element value in "<a>" tag with ruby


My goal is to find the first result in google search resultes and collect the site link, so I built this script:

require 'hpricot'
require 'open-uri'
require 'mechanize'
query = gets.chomp
agent = Mechanize.new
page = agent.get("http://www.google.co.il/")
search_form = page.form_with(:name => "f")
search_form.field_with(:name => "q").value = query.to_s
search_results = agent.submit(search_form)
search_results = search_results.body
doc = Hpricot(search_results)
site = doc.search("a")[16,1]
url = site.to_s
puts url

I get a string like this:

url = <a href="http://en.wikipedia.org/wiki/Gallon" dir="ltr" class="l"><em>Gallon</em> - Wikipedia, the free encyclopedia</a>

But I need only the link (http://en.wikipedia.org/wiki/Gallon) not all the html code... How can I do it? I am using the gems:

require 'hpricot'
require 'open-uri'
require 'mechanize'

Solution

  • You can get the value of attributes like this

    (doc/"a")[16].attributes['href']
    

    but I have to say that the magic number 16 seems brittle.

    You are also not supposed to scrape the search results, you should consider using the Custom Search API.