<?xml version="1.0" encoding="UTF-8"?>
<snippet>
  <account-id type="integer">2</account-id>
  <body>Note that these instructions don't work with the latest Scrubyt version...

Scrubyt is a Ruby library that allows you to easily scrape the contents of any site.

h2. First install Scrubyt:

&lt;code&gt;
$ sudo gem install mechanize hpricot parsetree ruby2ruby scrubyt
&lt;/code&gt;

You also need to install ReadLine version 3.6.3:

&lt;code&gt;
sudo gem install -v 3.6.3 RubyInline
&lt;/code&gt;

If you install the wrong RubyInline version or have multiple versions installed, you'll get the following error:

&lt;code&gt;
/usr/lib/ruby/1.8/rubygems.rb:207:in `activate': can't activate RubyInline (= 3.6.3), already activated RubyInline-3.6.6] (Gem::Exception)
       from /usr/lib/ruby/1.8/rubygems.rb:225:in `activate'
       from /usr/lib/ruby/1.8/rubygems.rb:224:in `each'
       from /usr/lib/ruby/1.8/rubygems.rb:224:in `activate'
       from /usr/lib/ruby/1.8/rubygems/custom_require.rb:32:in `require'
       from t2:2
&lt;/code&gt;

To fix it first uninstall the latest version, and keep only version 3.6.3:

&lt;code&gt;
sudo gem uninstall RubyInline

Select RubyGem to uninstall:
 1. RubyInline-3.6.3
 2. RubyInline-3.6.6
 3. All versions
&gt; 2
&lt;/code&gt;

h2. Scraping Google search results

Then run this to Scrape the first two pages of the Google results for *ruby*:

&lt;code&gt;
require 'rubygems'
require 'scrubyt'

# See http://scrubyt.org/example-specification-from-the-page-known-issues-and-pitfalls/

# Create a learning extractor
data = Scrubyt::Extractor.define do
  fetch('http://www.google.com/')
  fill_textfield 'q', 'ruby'
  submit
  
  # Teach Scrubyt what we want to retrieve
  # In this case we want Scruby to find all search results
  # and &quot;Ruby Programming Language&quot; happens to be the first 
  # link in the result list. Change &quot;Ruby Programming Language&quot; 
  # to whatever you want Scruby to find.
  link do
    name  &quot;Ruby Programming Language&quot;
    url   &quot;href&quot;, :type =&gt; :attribute
  end
  
  # Click next until we're on the second page.
  next_page &quot;Next&quot;, :limit =&gt; 2
end

# Print out what Scruby found
puts data.to_xml 

puts &quot;Your production scraper has been created: data_extractor_export.rb.&quot;

# Export the production version of the scraper
data.export(__FILE__)
&lt;/code&gt;

h2. Learning Extractor vs Production extractor

Note that this example uses the &quot;Learning Extractor&quot;:http://scrubyt.org/example-specification-from-the-page-known-issues-and-pitfalls/ functionality of Scrubyt.

The production extractor is generated with the last line:

&lt;code&gt;
data.export(__FILE__)
&lt;/code&gt;

If you open the production extractor in an editor you'll see that it uses XPath queries to extract the content:
&lt;code&gt;
  link(&quot;/html/body/div/div/div/h2&quot;, { :generalize =&gt; true }) do
    name(&quot;/a[1]&quot;)
    url(&quot;href&quot;, { :type =&gt; :attribute })
  end
&lt;/code&gt;

h2. Finding the correct XPath

The learning mode is pretty good at finding the XPath of HTML elements, but if you have difficulties getting Scrubyt to extract exactly what you want, simply install Firebug and use the Inspect feature to select the item you want to extract the value from. Then right-click on it in the Firebug window and choose Copy XPath.

Note that there's a &quot;gotcha&quot;:http://groups.google.com/group/firebug/browse_thread/thread/b3f9b0893c1ad7e1 when copying the XPath of an element with Firebug. Firebug uses Firefox's internal and normalized DOM model, which might not match match the real-world HTML structure. For example the tbody tag is usually added by Firefox/Firebug, and should be removed if it isn't in the HTML.

Another option that I haven't tried myself is to use the &quot;XPather&quot;:https://addons.mozilla.org/en-US/firefox/addon/1192 extension.

h2. Using hpricot to find the XPath

If you're really having problems finding the right XPath of an element, you can also use HPricot to find it. In this example the code prints out the XPath to all table columns containing the text 51,999:

&lt;code&gt;
require 'rexml/document'
require 'hpricot'
require 'open-uri'

url = &quot;http://xyz&quot;

page = Hpricot(open(url,
	'User-Agent' =&gt; 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12',
        'Referer'    =&gt; 'http://xyz'
    	))

page.search( &quot;//td:contains('51,992')&quot; ).each do |row|
  puts row.xpath()
end
&lt;/code&gt;

The output from the above snippet looks something like this:

&lt;code&gt;
/html/body/table[2]/tr[2]/td[3]
/html/body/table[2]/tr[2]/td[3]/table[4]/tr[1]/td[1]
/html/body/table[2]/tr[2]/td[3]/table[4]/tr[1]/td[1]/table[1]/tr[2]/td[2]
&lt;/code&gt;

Note that sometimes I find that hrpicot is easier to use than Scrubyt, so use what's best for you.

h2. Miscellaneous problems

The following problem can be solved by following the instructions found &quot;here&quot;:http://agora.scrubyt.org/forums/3/topics/270:
&lt;code&gt;
Your production scraper has been created: data_extractor_export.rb.
/var/lib/gems/1.8/gems/ParseTreeReloaded-0.0.1/lib/parse_tree_reloaded.rb:129:in `extend': wrong argument type Class (expected Module) (TypeError)
       from /var/lib/gems/1.8/gems/ParseTreeReloaded-0.0.1/lib/parse_tree_reloaded.rb:129:in `to_sexp'
       from /var/lib/gems/1.8/gems/ParseTreeReloaded-0.0.1/lib/parse_tree_reloaded.rb:93:in `parse_tree_for_method'
       from /var/lib/gems/1.8/gems/ruby2ruby-1.1.6/lib/ruby2ruby.rb:1063:in `to_sexp'
&lt;/code&gt;</body>
  <comments-count type="integer">0</comments-count>
  <created-at type="datetime">2007-10-15T14:07:52+03:00</created-at>
  <id type="integer">90</id>
  <language-id type="integer">124</language-id>
  <rendered-body>&lt;p&gt;Note that these instructions don&amp;#8217;t work with the latest Scrubyt version&amp;#8230;&lt;/p&gt;
&lt;p&gt;Scrubyt is a Ruby library that allows you to easily scrape the contents of any site.&lt;/p&gt;
&lt;h2&gt;First install Scrubyt:&lt;/h2&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; $ sudo gem install mechanize hpricot parsetree ruby2ruby scrubyt
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You also need to install ReadLine version 3.6.3:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; sudo gem install &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;v &lt;span class=&quot;Number&quot;&gt;3.6&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;RubyInline&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If you install the wrong RubyInline version or have multiple versions installed, you&amp;#8217;ll get the following error:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;usr&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;ruby&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.8&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;rubygems.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;207&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;activate': can't activate RubyInline (= 3.6.3), already activated RubyInline-3.6.6] (Gem::Exception)&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from /usr/lib/ruby/1.8/rubygems.rb:225:in &lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;&lt;/span&gt;activate&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from /usr/lib/ruby/1.8/rubygems.rb:224:in `each&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt;        from &lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;usr&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;ruby&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.8&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;rubygems.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;224&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;activate'&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from /usr/lib/ruby/1.8/rubygems/custom_require.rb:32:in &lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from t2:2&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To fix it first uninstall the latest version, and keep only version 3.6.3:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; sudo gem uninstall &lt;span class=&quot;Variable&quot;&gt;RubyInline&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Select&lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;RubyGem&lt;/span&gt; to uninstall:
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt;  &lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;. &lt;span class=&quot;Variable&quot;&gt;RubyInline&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;3.6&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt;  &lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;. &lt;span class=&quot;Variable&quot;&gt;RubyInline&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;3.6&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt;  &lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt;. &lt;span class=&quot;Variable&quot;&gt;All&lt;/span&gt; versions
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt;Scraping Google search results&lt;/h2&gt;
&lt;p&gt;Then run this to Scrape the first two pages of the Google results for &lt;strong&gt;ruby&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;rubygems&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;scrubyt&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; See http://scrubyt.org/example-specification-from-the-page-known-issues-and-pitfalls/&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Create a learning extractor&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; data &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;Scrubyt&lt;/span&gt;::&lt;span class=&quot;FunctionName&quot;&gt;Extractor&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt;   &lt;span class=&quot;FunctionName&quot;&gt;fetch&lt;/span&gt;(&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;http://www.google.com/&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;)
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt;   fill_textfield &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;q&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;ruby&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;   submit
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt;   
&lt;span class=&quot;line-numbers&quot;&gt;  12 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Teach Scrubyt what we want to retrieve&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  13 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; In this case we want Scruby to find all search results&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  14 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; and &amp;quot;Ruby Programming Language&amp;quot; happens to be the first &lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  15 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; link in the result list. Change &amp;quot;Ruby Programming Language&amp;quot; &lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  16 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; to whatever you want Scruby to find.&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  17 &lt;/span&gt;   link &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  18 &lt;/span&gt;     name  &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Ruby Programming Language&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  19 &lt;/span&gt;     url   &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;href&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;type&lt;/span&gt; =&amp;gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;attribute&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  20 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  21 &lt;/span&gt;   
&lt;span class=&quot;line-numbers&quot;&gt;  22 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;  &lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Click next until we're on the second page.&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  23 &lt;/span&gt;   next_page &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Next&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;limit&lt;/span&gt; =&amp;gt; &lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  24 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  25 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  26 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Print out what Scruby found&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  27 &lt;/span&gt; puts data.&lt;span class=&quot;FunctionName&quot;&gt;to_xml&lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  28 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  29 &lt;/span&gt; puts &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;Your production scraper has been created: data_extractor_export.rb.&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  30 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  31 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; Export the production version of the scraper&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  32 &lt;/span&gt; data.&lt;span class=&quot;FunctionName&quot;&gt;export&lt;/span&gt;(&lt;span class=&quot;Variable&quot;&gt;__FILE__&lt;/span&gt;)
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt;Learning Extractor vs Production extractor&lt;/h2&gt;
&lt;p&gt;Note that this example uses the &lt;a href=&quot;http://scrubyt.org/example-specification-from-the-page-known-issues-and-pitfalls/&quot;&gt;Learning Extractor&lt;/a&gt; functionality of Scrubyt.&lt;/p&gt;
&lt;p&gt;The production extractor is generated with the last line:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; data.&lt;span class=&quot;FunctionName&quot;&gt;export&lt;/span&gt;(&lt;span class=&quot;Variable&quot;&gt;__FILE__&lt;/span&gt;)
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If you open the production extractor in an editor you&amp;#8217;ll see that it uses XPath queries to extract the content:&lt;br /&gt;
&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;FunctionName&quot;&gt;link&lt;/span&gt;(&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;/html/body/div/div/div/h2&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, { &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;generalize&lt;/span&gt; =&amp;gt; &lt;span class=&quot;BuiltInConstant&quot;&gt;true&lt;/span&gt; }) &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt;     &lt;span class=&quot;FunctionName&quot;&gt;name&lt;/span&gt;(&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;/a[1]&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt;     &lt;span class=&quot;FunctionName&quot;&gt;url&lt;/span&gt;(&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;href&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, { &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;type&lt;/span&gt; =&amp;gt; &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;attribute&lt;/span&gt; })
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt;   &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;h2&gt;Finding the correct XPath&lt;/h2&gt;
&lt;p&gt;The learning mode is pretty good at finding the XPath of &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; elements, but if you have difficulties getting Scrubyt to extract exactly what you want, simply install Firebug and use the Inspect feature to select the item you want to extract the value from. Then right-click on it in the Firebug window and choose Copy XPath.&lt;/p&gt;
&lt;p&gt;Note that there&amp;#8217;s a &lt;a href=&quot;http://groups.google.com/group/firebug/browse_thread/thread/b3f9b0893c1ad7e1&quot;&gt;gotcha&lt;/a&gt; when copying the XPath of an element with Firebug. Firebug uses Firefox&amp;#8217;s internal and normalized &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; model, which might not match match the real-world &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; structure. For example the tbody tag is usually added by Firefox/Firebug, and should be removed if it isn&amp;#8217;t in the &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Another option that I haven&amp;#8217;t tried myself is to use the &lt;a href=&quot;https://addons.mozilla.org/en-US/firefox/addon/1192&quot;&gt;XPather&lt;/a&gt; extension.&lt;/p&gt;
&lt;h2&gt;Using hpricot to find the XPath&lt;/h2&gt;
&lt;p&gt;If you&amp;#8217;re really having problems finding the right XPath of an element, you can also use HPricot to find it. In this example the code prints out the XPath to all table columns containing the text 51,999:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;rexml/document&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;hpricot&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;open-uri&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; url &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;http://xyz&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; page &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Hpricot&lt;/span&gt;(&lt;span class=&quot;FunctionName&quot;&gt;open&lt;/span&gt;(url,
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt; 	&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;User-Agent&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt; =&amp;gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;,
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt;         &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;Referer&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;    =&amp;gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;http://xyz&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;     	))
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  12 &lt;/span&gt; page.&lt;span class=&quot;FunctionName&quot;&gt;search&lt;/span&gt;( &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;//td:contains('51,992')&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; ).&lt;span class=&quot;FunctionName&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;do &lt;/span&gt;|&lt;span class=&quot;Variable&quot;&gt;row&lt;/span&gt;|
&lt;span class=&quot;line-numbers&quot;&gt;  13 &lt;/span&gt;   puts row.&lt;span class=&quot;FunctionName&quot;&gt;xpath&lt;/span&gt;()
&lt;span class=&quot;line-numbers&quot;&gt;  14 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The output from the above snippet looks something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;body&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt;]
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;body&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;4&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;]
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;body&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;3&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;4&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;table[&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;tr[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;td[&lt;span class=&quot;Number&quot;&gt;2&lt;/span&gt;]
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Note that sometimes I find that hrpicot is easier to use than Scrubyt, so use what&amp;#8217;s best for you.&lt;/p&gt;
&lt;h2&gt;Miscellaneous problems&lt;/h2&gt;
&lt;p&gt;The following problem can be solved by following the instructions found &lt;a href=&quot;http://agora.scrubyt.org/forums/3/topics/270&quot;&gt;here&lt;/a&gt;:&lt;br /&gt;
&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Your&lt;/span&gt; production scraper has been created: data_extractor_export.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;.
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;/&lt;/span&gt;&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.8&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Variable&quot;&gt;ParseTreeReloaded&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;0.0&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;parse_tree_reloaded.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;129&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;extend': wrong argument type Class (expected Module) (TypeError)&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from /var/lib/gems/1.8/gems/ParseTreeReloaded-0.0.1/lib/parse_tree_reloaded.rb:129:in &lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;&lt;/span&gt;to_sexp&lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; &lt;span class=&quot;String&quot;&gt;       from /var/lib/gems/1.8/gems/ParseTreeReloaded-0.0.1/lib/parse_tree_reloaded.rb:93:in `parse_tree_for_method&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt;        from &lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;var&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.8&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;gems&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;ruby2ruby&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Number&quot;&gt;1.1&lt;/span&gt;.&lt;span class=&quot;Number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;ruby2ruby.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt;:&lt;span class=&quot;Number&quot;&gt;1063&lt;/span&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;in&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;`&lt;/span&gt;to_sexp'&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;</rendered-body>
  <title>Scraping Google search results with Scrubyt and Ruby</title>
  <updated-at type="datetime">2008-11-23T02:00:24+02:00</updated-at>
</snippet>
