-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlparser.go
360 lines (329 loc) · 10.7 KB
/
htmlparser.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package middleware
/* soup package implements a simple web scraper for Go,
keeping it as similar as possible to BeautifulSoup
*/
import (
"bytes"
"fmt"
"regexp"
"strings"
"golang.org/x/net/html"
)
// ErrorType defines types of errors that are possible from soup
type ErrorType int
const (
// ErrUnableToParse will be returned when the HTML could not be parsed
ErrUnableToParse ErrorType = iota
// ErrElementNotFound will be returned when element was not found
ErrElementNotFound
// ErrNoNextSibling will be returned when no next sibling can be found
ErrNoNextSibling
// ErrNoPreviousSibling will be returned when no previous sibling can be found
ErrNoPreviousSibling
// ErrNoNextElementSibling will be returned when no next element sibling can be found
ErrNoNextElementSibling
// ErrNoPreviousElementSibling will be returned when no previous element sibling can be found
ErrNoPreviousElementSibling
// ErrCreatingGetRequest will be returned when the get request couldn't be created
ErrCreatingGetRequest
// ErrInGetRequest will be returned when there was an error during the get request
ErrInGetRequest
// ErrCreatingPostRequest will be returned when the post request couldn't be created
ErrCreatingPostRequest
// ErrMarshallingPostRequest will be returned when the body of a post request couldn't be serialized
ErrMarshallingPostRequest
// ErrReadingResponse will be returned if there was an error reading the response to our get request
ErrReadingResponse
)
// Error allows easier introspection on the type of error returned.
// If you know you have a Error, you can compare the Type to one of the exported types
// from this package to see what kind of error it is, then further inspect the Error() method
// to see if it has more specific details for you, like in the case of a ErrElementNotFound
// type of error.
type Error struct {
Type ErrorType
msg string
}
func (se Error) Error() string {
return se.msg
}
func newError(t ErrorType, msg string) Error {
return Error{Type: t, msg: msg}
}
// Root is a structure containing a pointer to an html node, the node value, and an error variable to return an error if one occurred
type Root struct {
Pointer *html.Node
NodeValue string
Error error
}
// HTMLParse parses the HTML returning a start pointer to the DOM
func HTMLParse(s string) Root {
r, err := html.Parse(strings.NewReader(s))
if err != nil {
return Root{Error: newError(ErrUnableToParse, "unable to parse the HTML")}
}
for r.Type != html.ElementNode {
switch r.Type {
case html.DocumentNode:
r = r.FirstChild
case html.DoctypeNode:
r = r.NextSibling
case html.CommentNode:
r = r.NextSibling
}
}
return Root{Pointer: r, NodeValue: r.Data}
}
// Find finds the first occurrence of the given tag name,
// with or without attribute key and value specified,
// and returns a struct with a pointer to it
func (r Root) Find(args ...string) Root {
temp, ok := findOnce(r.Pointer, args, false, false)
if !ok {
return Root{Error: newError(ErrElementNotFound, fmt.Sprintf("element `%s` with attributes `%s` not found", args[0], strings.Join(args[1:], " ")))}
}
return Root{Pointer: temp, NodeValue: temp.Data}
}
// FindAll finds all occurrences of the given tag name,
// with or without key and value specified,
// and returns an array of structs, each having
// the respective pointers
func (r Root) FindAll(args ...string) []Root {
temp := findAllofem(r.Pointer, args, false)
if len(temp) == 0 {
return []Root{}
}
pointers := make([]Root, 0, len(temp))
for i := 0; i < len(temp); i++ {
pointers = append(pointers, Root{Pointer: temp[i], NodeValue: temp[i].Data})
}
return pointers
}
// FindStrict finds the first occurrence of the given tag name
// only if all the values of the provided attribute are an exact match
func (r Root) FindStrict(args ...string) Root {
temp, ok := findOnce(r.Pointer, args, false, true)
if !ok {
return Root{nil, "", newError(ErrElementNotFound, fmt.Sprintf("element `%s` with attributes `%s` not found", args[0], strings.Join(args[1:], " ")))}
}
return Root{Pointer: temp, NodeValue: temp.Data}
}
// FindAllStrict finds all occurrences of the given tag name
// only if all the values of the provided attribute are an exact match
func (r Root) FindAllStrict(args ...string) []Root {
temp := findAllofem(r.Pointer, args, true)
if len(temp) == 0 {
return []Root{}
}
pointers := make([]Root, 0, len(temp))
for i := 0; i < len(temp); i++ {
pointers = append(pointers, Root{Pointer: temp[i], NodeValue: temp[i].Data})
}
return pointers
}
// FindNextSibling finds the next sibling of the pointer in the DOM
// returning a struct with a pointer to it
func (r Root) FindNextSibling() Root {
nextSibling := r.Pointer.NextSibling
if nextSibling == nil {
return Root{Error: newError(ErrNoNextSibling, "no next sibling found")}
}
return Root{Pointer: nextSibling, NodeValue: nextSibling.Data}
}
// FindPrevSibling finds the previous sibling of the pointer in the DOM
// returning a struct with a pointer to it
func (r Root) FindPrevSibling() Root {
prevSibling := r.Pointer.PrevSibling
if prevSibling == nil {
return Root{Error: newError(ErrNoPreviousSibling, "no previous sibling found")}
}
return Root{Pointer: prevSibling, NodeValue: prevSibling.Data}
}
// FindNextElementSibling finds the next element sibling of the pointer in the DOM
// returning a struct with a pointer to it
func (r Root) FindNextElementSibling() Root {
nextSibling := r.Pointer.NextSibling
if nextSibling == nil {
return Root{Error: newError(ErrNoNextElementSibling, "no next element sibling found")}
}
if nextSibling.Type == html.ElementNode {
return Root{Pointer: nextSibling, NodeValue: nextSibling.Data}
}
p := Root{Pointer: nextSibling, NodeValue: nextSibling.Data}
return p.FindNextElementSibling()
}
// FindPrevElementSibling finds the previous element sibling of the pointer in the DOM
// returning a struct with a pointer to it
func (r Root) FindPrevElementSibling() Root {
prevSibling := r.Pointer.PrevSibling
if prevSibling == nil {
return Root{Error: newError(ErrNoPreviousElementSibling, "no previous element sibling found")}
}
if prevSibling.Type == html.ElementNode {
return Root{Pointer: prevSibling, NodeValue: prevSibling.Data}
}
p := Root{Pointer: prevSibling, NodeValue: prevSibling.Data}
return p.FindPrevElementSibling()
}
// Children returns all direct children of this DOME element.
func (r Root) Children() []Root {
child := r.Pointer.FirstChild
var children []Root
for child != nil {
children = append(children, Root{Pointer: child, NodeValue: child.Data})
child = child.NextSibling
}
return children
}
// Attrs returns a map containing all attributes
func (r Root) Attrs() map[string]string {
if r.Pointer.Type != html.ElementNode {
return nil
}
if len(r.Pointer.Attr) == 0 {
return nil
}
return getKeyValue(r.Pointer.Attr)
}
// Text returns the string inside a non-nested element
func (r Root) Text() string {
k := r.Pointer.FirstChild
checkNode:
if k != nil && k.Type != html.TextNode {
k = k.NextSibling
if k == nil {
return ""
}
goto checkNode
}
if k != nil {
r, _ := regexp.Compile(`^\s+$`)
if ok := r.MatchString(k.Data); ok {
k = k.NextSibling
if k == nil {
return ""
}
goto checkNode
}
return k.Data
}
return ""
}
// HTML returns the HTML code for the specific element
func (r Root) HTML() string {
var buf bytes.Buffer
if err := html.Render(&buf, r.Pointer); err != nil {
return ""
}
return buf.String()
}
// FullText returns the string inside even a nested element
func (r Root) FullText() string {
var buf bytes.Buffer
var f func(*html.Node)
f = func(n *html.Node) {
if n == nil {
return
}
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
if n.Type == html.ElementNode {
f(n.FirstChild)
}
if n.NextSibling != nil {
f(n.NextSibling)
}
}
f(r.Pointer.FirstChild)
return buf.String()
}
func matchElementName(n *html.Node, name string) bool {
return name == "" || name == n.Data
}
// Using depth first search to find the first occurrence and return
func findOnce(n *html.Node, args []string, uni bool, strict bool) (*html.Node, bool) {
if uni == true {
if n.Type == html.ElementNode && matchElementName(n, args[0]) {
if len(args) > 1 && len(args) < 4 {
for i := 0; i < len(n.Attr); i++ {
attr := n.Attr[i]
searchAttrName := args[1]
searchAttrVal := args[2]
if (strict && attributeAndValueEquals(attr, searchAttrName, searchAttrVal)) ||
(!strict && attributeContainsValue(attr, searchAttrName, searchAttrVal)) {
return n, true
}
}
} else if len(args) == 1 {
return n, true
}
}
}
uni = true
for c := n.FirstChild; c != nil; c = c.NextSibling {
p, q := findOnce(c, args, true, strict)
if q != false {
return p, q
}
}
return nil, false
}
// Using depth first search to find all occurrences and return
func findAllofem(n *html.Node, args []string, strict bool) []*html.Node {
var nodeLinks = make([]*html.Node, 0, 10)
var f func(*html.Node, []string, bool)
f = func(n *html.Node, args []string, uni bool) {
if uni == true {
if n.Type == html.ElementNode && matchElementName(n, args[0]) {
if len(args) > 1 && len(args) < 4 {
for i := 0; i < len(n.Attr); i++ {
attr := n.Attr[i]
searchAttrName := args[1]
searchAttrVal := args[2]
if (strict && attributeAndValueEquals(attr, searchAttrName, searchAttrVal)) ||
(!strict && attributeContainsValue(attr, searchAttrName, searchAttrVal)) {
nodeLinks = append(nodeLinks, n)
}
}
} else if len(args) == 1 {
nodeLinks = append(nodeLinks, n)
}
}
}
uni = true
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c, args, true)
}
}
f(n, args, false)
return nodeLinks
}
// attributeAndValueEquals reports when the html.Attribute attr has the same attribute name and value as from
// provided arguments
func attributeAndValueEquals(attr html.Attribute, attribute, value string) bool {
return attr.Key == attribute && attr.Val == value
}
// attributeContainsValue reports when the html.Attribute attr has the same attribute name as from provided
// attribute argument and compares if it has the same value in its values parameter
func attributeContainsValue(attr html.Attribute, attribute, value string) bool {
if attr.Key == attribute {
for _, attrVal := range strings.Fields(attr.Val) {
if attrVal == value {
return true
}
}
}
return false
}
// Returns a key pair value (like a dictionary) for each attribute
func getKeyValue(attributes []html.Attribute) map[string]string {
var keyvalues = make(map[string]string)
for i := 0; i < len(attributes); i++ {
_, exists := keyvalues[attributes[i].Key]
if exists == false {
keyvalues[attributes[i].Key] = attributes[i].Val
}
}
return keyvalues
}