search_node.go
This commit is contained in:
40
search_node.go
Normal file
40
search_node.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package scraper
|
||||
|
||||
import "golang.org/x/net/html"
|
||||
|
||||
func MatchNode(n1 *html.Node, n2 *html.Node) bool {
|
||||
if n1.Data == n2.Data {
|
||||
if len(n1.Attr) == 0 ||
|
||||
len(n2.Attr) == 0 {
|
||||
return false
|
||||
}
|
||||
for i, a1 := range n1.Attr {
|
||||
a2 := n2.Attr[i]
|
||||
return a1.Key == a2.Key
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SearchNode(target *html.Node, n *html.Node) chan *html.Node {
|
||||
ch := make(chan *html.Node)
|
||||
var crawl crawlFunc
|
||||
crawl = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
if MatchNode(target, n) {
|
||||
ch <- n
|
||||
}
|
||||
}
|
||||
c := n.FirstChild
|
||||
for c != nil {
|
||||
crawl(c)
|
||||
c = c.NextSibling
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
defer close(ch)
|
||||
crawl(n)
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user