From: eden li Date: 2007-05-22T11:36:14+09:00 Subject: Re: How to delete a node with Hpricot? I can't comment on the error, but you can delete a given node in your traverse_element block by doing node.parent.children.delete(node). Alternatively, you can run a search first to remove the "really bad elements." If you define really bad elements in terms of an xpath query, this becomes very simple: (doc/"script").remove Then you can go through and swap things out as necessary: (doc/"xpath to bad nodes").each do |el| el.inner_html = "" end On May 21, 8:04 pm, "Daniel N" wrote: > Hi, > > Sorry if this is not the right forum for this question. > > If a node is bad I'm trying to comment it out. If it's really bad > I'm trynig to delete it. > > The way I'm trying to do is is as follows. > > def this_is_a_problem > doc = Hpricot( html ) > doc.traverse_element do |node| > if some_bad_node_test > unless really_bad? > node.swap( "" ) > else > node.swap( "" ) > end > end > doc.to_html > end > > However I'm getting a nasty error. > > TypeError: no implicit conversion from nil to integer > /usr/local/lib/ruby/gems/1.8/gems/hpricot-0.5/lib/hpricot/traverse.rb:395:in > `[]=' > > Am I doing this the wrong way? > > Thanx > > Daniel