How to delete a node with Hpricot?

Discussion in 'Ruby' started by Daniel N, May 21, 2007.

  1. Daniel N

    Daniel N Guest

    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( "<!-- comment out node #{node.to_html} -->" )
    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
    Daniel N, May 21, 2007
    #1
    1. Advertising

  2. Daniel N

    eden li Guest

    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 = "<!-- #{el.to_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( "<!-- comment out node #{node.to_html} -->" )
    > 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
    eden li, May 22, 2007
    #2
    1. Advertising

  3. Daniel N

    Daniel N Guest

    On 5/22/07, eden li <> wrote:
    > 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 = "<!-- #{el.to_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( "<!-- comment out node #{node.to_html} -->" )
    > > 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

    >
    >
    >


    Sweet, that looks like a good way to go. I'll try that out.

    I've actually got it working but it's very very slow, hopefully this
    will speed it up

    Thankyou
    Daniel
    Daniel N, May 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wolf
    Replies:
    0
    Views:
    544
  2. Replies:
    0
    Views:
    1,411
  3. Replies:
    0
    Views:
    362
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,403
    Dimitre Novatchev
    Aug 24, 2006
  5. sangram
    Replies:
    16
    Views:
    1,966
Loading...

Share This Page