开发者

How to remove a particular content inside a div using Hpricot

开发者 https://www.devze.com 2023-03-09 23:59 出处:网络
I have the following html structure <div id=\"rn_answertext\"> <p>asdasdasdas</p> <p>asdasdas开发者_StackOverflowdas</p>

I have the following html structure

  <div id="rn_answertext">
    <p>asdasdasdas</p>
    <p>asdasdas开发者_StackOverflowdas</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.


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


div = doc.search("//div[@id='rn_answertext']")

div.search('//div[@id="test"]').remove.html

Will give you the rn_answertext div content except test div.

0

精彩评论

暂无评论...
验证码 换一张
取 消