-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
89 lines (69 loc) · 1.7 KB
/
parse.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
package sitescraper
import (
"io/ioutil"
"net/http"
"strings"
)
//Sends a Get-Request to an URL and returns a string of the DOM that can be parsed with func ParseHTML
func Get(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
html, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
res.Body.Close()
return string(html), nil
}
//Parses HTML-String and returns an accessible Dom
func ParseHTML(html string) Dom {
return fetch(html)
}
//The private functions handle the parsing process
func fetch(html string) Dom {
var dm Dom
for {
if !strings.Contains(html, "<") {
break
}
tagname, tagcontent := getTagnameAndContent(html)
html = html[strings.Index(html, tagcontent)+len(tagcontent):]
//Check if string behing "<" is a valid tagname
if checkTagname(tagname) {
dm.Tag = append(dm.Tag, Tag{tagname, tagcontent, strings.ReplaceAll(getInnerHTML(tagname, html), breakSubst, "<br>")})
}
}
dm = clearClosingTags(dm)
dm.isparsed = true
return dm
}
func clearClosingTags(d Dom) Dom {
var out Dom
for _, n := range d.Tag {
if !strings.Contains(n.tagcontent, "</") {
out.Tag = append(out.Tag, n)
}
}
return out
}
func getTagnameAndContent(html string) (string, string) {
tagcontent := html[strings.Index(html, "<"):]
tagcontent = tagcontent[:strings.Index(tagcontent, ">")+1]
var tagname string
if strings.Contains(tagcontent, " ") {
tagname = tagcontent[1:strings.Index(tagcontent, " ")]
} else {
tagname = tagcontent[1:strings.Index(tagcontent, ">")]
}
return tagname, tagcontent
}
func checkTagname(tagname string) bool {
for _, n := range tagnames {
if tagname == n {
return true
}
}
return false
}