Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I parse this data structure returned by Nokogiri in Ruby?
    primarykey
    data
    text
    <p>So I am cycling through an array element and this is the result returned:</p> <pre><code>[nil, [#&lt;Nokogiri::XML::Element:0x835386d4 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x835385f8 name="href" value="http://bham.craigslist.org/web/2961573018.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x835381c0 "Web Designer Full time"&gt;]&gt; </code></pre> <p>What I would like to do is access <code>href</code> value, and then the <code>text</code> value. How do I do that?</p> <p>I tried this:</p> <pre><code>puts i[:href] </code></pre> <p>But that generates this error:</p> <pre><code>TypeError: Symbol as array index </code></pre> <p>By the way, I am accessing <code>i</code> as an element in the array via each like this:</p> <pre><code>contents.each do |i| puts i.inspect puts i[:href] end </code></pre> <p><strong>Edit 1:</strong></p> <p>This is how I am generating the <code>contents</code> array. There is no need to rename it, because it can get confusing :)</p> <pre><code>contents = {} first_items.each do |link| content_url = link content_page = Nokogiri::HTML(open(content_url)) contents[link[:href]] = content_page.css("p a") end puts contents.inspect </code></pre> <p>This is what gets output:</p> <pre><code>{nil=&gt;[#&lt;Nokogiri::XML::Element:0x85fee914 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fee838 name="href" value="http://bham.craigslist.org/web/2961573018.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fee400 "Web Designer Full time"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fee298 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fee1bc name="href" value="http://bham.craigslist.org/web/2959813303.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fedd84 "Once in a lifetime opportunity..."&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fedc1c name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fedb40 name="href" value="http://bham.craigslist.org/web/2925485723.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fed708 "Website Designer and Blogging Internship!"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fed5a0 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fed4c4 name="href" value="http://bham.craigslist.org/web/2918424652.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fed08c "Excellent Java Developer Opportunity!"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fecf24 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fece48 name="href" value="http://bham.craigslist.org/web/2888669703.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85feca10 "Freelance Graphic Design"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fec8a8 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fec7cc name="href" value="http://bham.craigslist.org/web/2900256461.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fec394 "GWT/GXT Developer"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fec22c name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fec150 name="href" value="http://bham.craigslist.org/web/2897641463.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85febd18 "Website hiring!"&gt;]&gt;]} </code></pre> <p>Here is the full value of the output for <code>i</code>:</p> <pre><code>-------------------- This is the value of i: [nil, [#&lt;Nokogiri::XML::Element:0x85fee914 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fee838 name="href" value="http://bham.craigslist.org/web/2961573018.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fee400 "Web Designer Full time"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fee298 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fee1bc name="href" value="http://bham.craigslist.org/web/2959813303.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fedd84 "Once in a lifetime opportunity..."&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fedc1c name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fedb40 name="href" value="http://bham.craigslist.org/web/2925485723.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fed708 "Website Designer and Blogging Internship!"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fed5a0 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fed4c4 name="href" value="http://bham.craigslist.org/web/2918424652.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fed08c "Excellent Java Developer Opportunity!"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fecf24 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fece48 name="href" value="http://bham.craigslist.org/web/2888669703.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85feca10 "Freelance Graphic Design"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fec8a8 name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fec7cc name="href" value="http://bham.craigslist.org/web/2900256461.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85fec394 "GWT/GXT Developer"&gt;]&gt;, #&lt;Nokogiri::XML::Element:0x85fec22c name="a" attributes=[#&lt;Nokogiri::XML::Attr:0x85fec150 name="href" value="http://bham.craigslist.org/web/2897641463.html"&gt;] children=[#&lt;Nokogiri::XML::Text:0x85febd18 "Website hiring!"&gt;]&gt;]] -------------------- This is the value of i.href: </code></pre> <p><strong>Edit 2:</strong></p> <p>By the way, this is what the actual HTML output looks like...I did this:</p> <pre><code>builder = Nokogiri::HTML::Builder.new do |doc| doc.html { doc.body { contents.each do |el| if !el.nil? puts "-" * 20 puts "This is the value of el: " puts el.inspect puts "-" * 20 puts "This is the value of el.href: " puts el[:href] end doc.p { doc.a el, :href =&gt; el } end } } end puts "*" * 50 puts "This is the HTML generated" puts builder.to_html </code></pre> <p>This is how it looks:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt; &lt;html&gt;&lt;body&gt;&lt;p&gt;&lt;a href="&amp;lt;a%20href=%22http://bham.craigslist.org/web/2961573018.html%22&amp;gt;Web%20Designer%20Full%20time&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2959813303.html%22&amp;gt;Once%20in%20a%20lifetime%20opportunity...&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2925485723.html%22&amp;gt;Website%20Designer%20and%20Blogging%20Internship!&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2918424652.html%22&amp;gt;Excellent%20Java%20Developer%20Opportunity!&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2888669703.html%22&amp;gt;Freelance%20Graphic%20Design&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2900256461.html%22&amp;gt;GWT/GXT%20Developer&amp;lt;/a&amp;gt;&amp;lt;a%20href=%22http://bham.craigslist.org/web/2897641463.html%22&amp;gt;Website%20hiring!&amp;lt;/a&amp;gt;"&gt;&amp;lt;a href="http://bham.craigslist.org/web/2961573018.html"&amp;gt;Web Designer Full time&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2959813303.html"&amp;gt;Once in a lifetime opportunity...&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2925485723.html"&amp;gt;Website Designer and Blogging Internship!&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2918424652.html"&amp;gt;Excellent Java Developer Opportunity!&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2888669703.html"&amp;gt;Freelance Graphic Design&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2900256461.html"&amp;gt;GWT/GXT Developer&amp;lt;/a&amp;gt;&amp;lt;a href="http://bham.craigslist.org/web/2897641463.html"&amp;gt;Website hiring!&amp;lt;/a&amp;gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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