forked from sundy-li/html2article
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
210 lines (190 loc) · 4.32 KB
/
extract.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
package html2article
import (
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type extractor struct {
data map[*Info]*html.Node
urlStr string
doc *html.Node
maxDens float64
sn float64
swn float64
}
func NewFromHtml(htmlStr string) (ext *extractor, err error) {
return NewFromReader(strings.NewReader(htmlStr))
}
func NewFromReader(reader io.Reader) (ext *extractor, err error) {
doc, err := html.Parse(reader)
if err != nil {
return
}
return NewFromNode(doc)
}
func NewFromNode(doc *html.Node) (ext *extractor, err error) {
ext = &extractor{data: make(map[*Info]*html.Node), doc: doc}
return
}
func NewFromUrl(urlStr string) (ext *extractor, err error) {
resp, err := http.Get(urlStr)
if err != nil {
return
}
defer resp.Body.Close()
bs, _ := ioutil.ReadAll(resp.Body)
htmlStr := string(bs)
htmlStr = DecodeHtml(resp.Header, htmlStr, htmlStr)
ext, err = NewFromHtml(htmlStr)
if err != nil {
return
}
ext.urlStr = urlStr
return
}
var (
ERROR_NOTFOUND = errors.New("Content not found")
)
func (ec *extractor) ToArticle() (article *Article, err error) {
body := find(ec.doc, isTag(atom.Body))
if body == nil {
body = ec.doc
}
ec.getSn()
ec.getInfo(body)
node, err := ec.getBestMatch()
if err != nil {
return
}
if node == nil {
err = ERROR_NOTFOUND
return
}
ec.denoise(node)
article = &Article{}
// Get the Content
article.contentNode = node
article.Content = getText(node)
article.Html, err = getHtml(node)
if err != nil {
return
}
article.Images = getImages(node)
pnode := node.Parent
filterNode := func(n *html.Node) bool {
return n != node
}
for i := 0; i < 6 && pnode != nil; i++ {
article.Publishtime = getTime(getText(pnode, filterNode))
if article.Publishtime > 0 {
break
}
pnode = pnode.Parent
}
if article.Publishtime == 0 {
article.Publishtime = getTime(article.Content)
}
titleNode := find(ec.doc, isTag(atom.Title))
if titleNode != nil {
article.Title = getText(titleNode)
}
article.Images = getImages(node)
return
}
func (ec *extractor) getSn() {
txt := text(ec.doc)
ec.swn = float64(countStopWords(txt))
ec.sn = float64(countSn(txt))
}
func (ec *extractor) getInfo(node *html.Node) (info *Info) {
info = NewInfo()
if node.Type == html.TextNode {
info.TextCount = len(node.Data)
info.LeafList = append(info.LeafList, info.TextCount)
info.Data = node.Data
ec.addNode(node, info)
return
} else if node.Type == html.ElementNode {
if isTag(atom.Style)(node) || isTag(atom.Script)(node) {
return
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
cInfo := ec.getInfo(c)
info.TextCount += cInfo.TextCount
info.LinkTextCount += cInfo.LinkTextCount
info.TagCount += cInfo.TagCount
info.LinkTagCount += cInfo.LinkTagCount
info.LeafList = append(info.LeafList, cInfo.LeafList...)
info.Data += cInfo.Data
info.Pcount += cInfo.Pcount
info.ImageCount += cInfo.ImageCount
info.InputCount += cInfo.InputCount
}
info.TagCount++
switch node.DataAtom {
case atom.A:
info.LinkTagCount++
if node.Parent.DataAtom != atom.P {
info.LinkTextCount += len(info.Data)
}
case atom.P:
info.Pcount++
case atom.Img, atom.Image:
info.ImageCount++
case atom.Input, atom.Textarea, atom.Button:
info.InputCount++
}
if isContentNode(node) {
ec.addNode(node, info)
}
return
}
return
}
//正文去噪
//dmin 最小文本密度为正文最大文本密度的3/10
//去噪即删掉正文中文本密度小于dmin的非文本节点
func (ec *extractor) denoise(node *html.Node) {
dmin := ec.maxDens * 0.3
walk(node, func(n *html.Node) bool {
if isNoisingNode(n) {
info := ec.getInfo(n)
if info.Density < dmin {
next := n.NextSibling
n.Parent.RemoveChild(n)
n.NextSibling = next
return false
}
}
return true
})
}
func (ec *extractor) addNode(node *html.Node, info *Info) {
info.node = node
info.CalScore(ec.sn, ec.swn)
ec.data[info] = node
}
func (ec *extractor) getBestMatch() (node *html.Node, err error) {
if len(ec.data) < 1 {
err = ERROR_NOTFOUND
return
}
var maxScore float64 = -100
for kinfo, v := range ec.data {
if kinfo.score >= maxScore {
maxScore = kinfo.score
node = v
}
if kinfo.Density > ec.maxDens {
ec.maxDens = kinfo.Density
}
}
if node == nil {
err = ERROR_NOTFOUND
}
return
}