Using Hpricot to get children elements
If I have the following element with children elements inside, how do I go about accessing those children elements using Hpricot?
<p><b>Code</b> <i>base</开发者_如何学Ci> is <a href="#">cool</a> stuff dude!</p>
By "children", do you mean direct children, or all children recursively?
You can get direct children easily.
irb(main):038:0> (doc/"p").first.children
=> [{elem <b> "Code" </b>}, " ", {elem <i> "base" </i>}, " is ", {elem <a href="#"> "cool" </a>}, " stuff dude!"]
irb(main):039:0>
If you want any particular one, also easy:
irb(main):057:0> (doc/"p").first.find_element "b"
=> {elem <b> "Code" </b>}
irb(main):058:0> (doc/"p").first.find_element "a"
=> {elem <a href="#"> "cool" </a>}
irb(main):059:0>
More details would help us help you!
精彩评论