Git Product home page Git Product logo

Comments (3)

aaudis avatar aaudis commented on September 26, 2024 1

Thanks for your reply

Tonight I am not sleeping heh..

Actually I found solution how to remove specific node from tree

And this is what I was looking for

package main

import (
    "code.google.com/p/go.net/html"
    "fmt"
    "github.com/PuerkitoBio/goquery"
    "log"
)

//
// Searching node siblings (and child siblings and so on) and after successfull found - remove it
//
func RemoveNode(root_node *html.Node, remove_me *html.Node) {
    found_node := false
    check_nodes := make(map[int]*html.Node)
    i := 0

    // loop through siblings
    for n := root_node.FirstChild; n != nil; n = n.NextSibling {
        if n == remove_me {
            found_node = true
            n.Parent.RemoveChild(n)
        }

        check_nodes[i] = n
        i++
    }

    // check if removing node is found
    // if yes no need to check childs returning
    // if no continue loop through childs and so on
    if found_node == false {
        for _, item := range check_nodes {
            RemoveNode(item, remove_me)
        }
    }
}

//
// main
//
func main() {
    // get document
    dom, e := goquery.NewDocument(`some_kind_of_url`)
    if e != nil {
        log.Printf(`%s`, e)
        return
    }

    // make selection
    sel := dom.Find(`div[id=article-content]`)

    // remove all found elements from selection
    sel.Find(`div[class=news-top-block],script`).Each(func(i int, s *goquery.Selection) {
        RemoveNode(sel.Get(0), s.Get(0))
    })

    // print html
    html, _ := sel.Html()
    fmt.Println(html)
}

from goquery.

mna avatar mna commented on September 26, 2024

Not() doesn't do what you think it does. It's a filtering function on the current selection, and since your current selection is a "h1", applying Not("a") does nothing (it doesn't filter out any node, since the only nodes in the selection are "h1"s). It's not a DOM removal function.

There's no DOM manipulation functions in goquery at the moment, because there's no real DOM implementation in Go and there's not much we can do with the nodes once modified.

from goquery.

mna avatar mna commented on September 26, 2024

Yes, you can always get by with the lower-level html package. Happy hacking!

from goquery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.