I have HTML code like this:
<div id="first">
<dt>Label1</dt>
<dd>Value1</dd>
<dt>Label2</dt>
<dd>Value2</dd>
...
</div>
My code does not work.
doc.css("first").each do |item|
label = item.css("dt")
value = item.css("dd")
end
Show all the <dt>
tags firsts and then the <dd>
tags and I need "label: value"
After looking at the other answer here is an inefficient way of doing the same thing.
In css to reference the ID you need to put the # symbol before. For a class it's the . symbol.
Under the assumption that some
<dt>
may have multiple<dd>
, you want to find all<dt>
and then (for each) find the following<dd>
before the next<dt>
. This is pretty easy to do in pure Ruby, but more fun to do in just XPath. ;)Given this setup:
Using no XPath:
Using a Little XPath:
Using Lotsa XPath:
First of all, your HTML should have the
<dt>
and<dd>
elements inside a<dl>
:but that won't change how you parse it. You want to find the
<dt>
s and iterate over them, then at each<dt>
you can usenext_element
to get the<dd>
; something like this:That should work as long as the structure matches your example.