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”
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_elementto get the<dd>; something like this:That should work as long as the structure matches your example.