-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselectionpath.go
82 lines (57 loc) · 1.38 KB
/
selectionpath.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package articletext
import (
"strings"
"github.com/PuerkitoBio/goquery"
)
/*
Extract text by DOM path, aka jquery style
*/
func getTextByPathFromDocument(doc *goquery.Document, path string) (string, error) {
sel := doc.Find(path)
if sel != nil {
return getTextFromHtml(sel), nil
}
return "nothing", nil
}
// this function returns some specific signature of a selection
// so it can be easy found to get data quickly next time
func getSelectionSignature(s *goquery.Selection) string {
var signature string
tag, _ := goquery.OuterHtml(s)
pos := strings.Index(tag, ">")
if pos > -1 {
tag = tag[1:pos]
} else {
return ""
}
signature = convertTagToJqueryFormat(tag, s)
s.Parents().Each(func(i int, sec *goquery.Selection) {
ohtml, _ := goquery.OuterHtml(sec)
pos := strings.Index(ohtml, ">")
if pos > -1 {
ohtml = ohtml[1:pos]
}
tag := convertTagToJqueryFormat(ohtml, sec)
signature = tag + " " + signature
})
return signature
}
func convertTagToJqueryFormat(tag string, s *goquery.Selection) string {
tagitself := tag
pos := strings.Index(tag, " ")
if pos > -1 {
tagitself = tag[0:pos]
} else {
return tag
}
class, found := s.Attr("class")
if found && class != "" {
pos := strings.Index(class, " ")
// leave only a first class from a list
if pos > -1 {
class = class[0:pos]
}
tagitself = tagitself + "." + class
}
return tagitself
}