Search code examples
rubyhpricot

How to remove a particular content inside a div using Hpricot


I have the following html structure

  <div id="rn_answertext">
    <p>asdasdasdas</p>
    <p>asdasdasdas</p>
    <p>asdasdasdas</p>
    <h3>asdasdasdas</h3>
    <div id="test">Content to be excluded</div>
  </div>

What I need is, when I search for div id="rn_answertext" I need to get all the contents except that in the div with id=test

My current code is

  result = doc.search("div#rn_answertext").inner_html

Anyone please help.


Solution

  • First get the div you want, then find the div inside you'd like to remove:

    div = (doc/"div#rn_answertext")
    (div/"#test").remove
    puts div.to_s