Compare commits
1 Commits
78985687a1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 55a6a4d3ef |
38
match.go
Normal file
38
match.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package scraper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
"golang.org/x/net/html/atom"
|
||||||
|
)
|
||||||
|
|
||||||
|
var body = &html.Node{
|
||||||
|
Type: html.ElementNode,
|
||||||
|
Data: "body",
|
||||||
|
DataAtom: atom.Body,
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrNotAnElementNode = errors.New("not an ElementNode")
|
||||||
|
|
||||||
|
func MatchElemAttr(s string, n2 *html.Node) (bool, error) {
|
||||||
|
r := strings.NewReader(s)
|
||||||
|
n, err := html.ParseFragment(r, body)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
n1 := n[0]
|
||||||
|
|
||||||
|
if !(n1.Type == html.ElementNode &&
|
||||||
|
n2.Type == html.ElementNode) {
|
||||||
|
return false, ErrNotAnElementNode
|
||||||
|
}
|
||||||
|
|
||||||
|
if n1.Data == n2.Data &&
|
||||||
|
slices.Equal(n1.Attr, n2.Attr) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
26
match_test.go
Normal file
26
match_test.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package scraper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
)
|
||||||
|
|
||||||
|
const fragment = `<div id="main-copy"></div>`
|
||||||
|
|
||||||
|
func TestMatchElemAttr(t *testing.T) {
|
||||||
|
n, err := html.ParseFragment(strings.NewReader(fragment), body)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
n1 := n[0]
|
||||||
|
|
||||||
|
result, err := MatchElemAttr(fragment, n1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !result {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ func TestSearchElem(t *testing.T) {
|
|||||||
html.Parse(strings.NewReader(htmlStr)),
|
html.Parse(strings.NewReader(htmlStr)),
|
||||||
)
|
)
|
||||||
|
|
||||||
ch := searchElem(doc, "span")
|
ch := SearchElem(doc, "span")
|
||||||
count := 0
|
count := 0
|
||||||
for range ch {
|
for range ch {
|
||||||
count++
|
count++
|
||||||
@@ -31,7 +31,7 @@ func TestSearchAttr(t *testing.T) {
|
|||||||
html.Parse(strings.NewReader(htmlStr)),
|
html.Parse(strings.NewReader(htmlStr)),
|
||||||
)
|
)
|
||||||
|
|
||||||
ch := searchAttr(doc, "id", "test")
|
ch := SearchAttr(doc, "id", "test")
|
||||||
count := 0
|
count := 0
|
||||||
for range ch {
|
for range ch {
|
||||||
count++
|
count++
|
||||||
@@ -47,7 +47,7 @@ func TestSearchElemAttr(t *testing.T) {
|
|||||||
html.Parse(strings.NewReader(htmlStr)),
|
html.Parse(strings.NewReader(htmlStr)),
|
||||||
)
|
)
|
||||||
|
|
||||||
ch := searchElemAttr(doc, "span", "id", "test")
|
ch := SearchElemAttr(doc, "span", "id", "test")
|
||||||
count := 0
|
count := 0
|
||||||
for range ch {
|
for range ch {
|
||||||
count++
|
count++
|
||||||
|
|||||||
Reference in New Issue
Block a user