Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse inner_html inside for loop using XPath with nokogiri
    text
    copied!<p>I'm having troubles parsing inside a for loop of only the inner_html that I have found. I want to use XPath again inside that content only. I'm new to ruby so better solutions are on the table.</p> <pre><code>#!/usr/bin/ruby -w require 'rubygems' require 'nokogiri' page1 = Nokogiri::HTML(open('mycontacts.html')) # Search for nodes by xpath page1.xpath('//html/body/form/div[2]/span/table/tbody/tr').each do |row| #puts a_tag.content puts "new row" row_html = row.inner_html puts row_html puts "" name = row_html.xpath("/td[1]").text puts "name is " + name end </code></pre> <p>My output of each row in the for loop is something like:</p> <pre><code>new row &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;Phone&lt;/th&gt; </code></pre> <p>Here's the error that I'm getting:</p> <p>screen-scraper.rb:20:in <code>block in &lt;main&gt;': undefined method</code>xpath' for # (NoMethodError)</p> <p>I want to parse each tr and get data like: Barney Rubble, Fred Flintstone</p> <pre><code>&lt;table&gt; &lt;tbody&gt; &lt;tr&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fred&lt;/td&gt; &lt;td&gt;Flintstone&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Barney&lt;/td&gt; &lt;td&gt;Rubble&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I'm open to suggestions. I was thinking it's easier to parse only the inner_html inside the for loop, but if there's an easier way to get at the node within the for loop, that would work as well.</p> <p>Thanks....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload