-
Notifications
You must be signed in to change notification settings - Fork 4
/
title.go
62 lines (54 loc) · 1.26 KB
/
title.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
package textractor
import (
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
var (
titleRx = regexp.MustCompile("_|\\|")
titlePx = []string{
"articletitle",
":title",
"title",
}
)
// titleExtract 提取文章作者
// source 网页源码
// content 正文
func titleExtract(headText []*headEntry, source *goquery.Selection, content *goquery.Selection) string {
var title string
for _, v := range headText {
for _, px := range titlePx {
if strings.Contains(v.key, px) {
if title = strings.TrimSpace(titleRx.Split(v.val, -1)[0]); title != "" {
return title
}
}
}
}
titleNode := source.Find("title")
if titleNode.Length() > 0 {
title = strings.TrimSpace(titleRx.Split(titleNode.Text(), -1)[0])
if title != "" {
return title
}
}
title = titleExtractFromHTML(content)
return title
}
// titleExtractFromHTML 从html标签中查找
func titleExtractFromHTML(content *goquery.Selection) string {
title, _ := findHtag(content)
return title
}
func findHtag(content *goquery.Selection) (string, bool) {
parent := content.Parent()
htag := parent.Find("h1,h2,h3,h4")
if htag.Length() > 0 {
return htag.Eq(0).Text(), true
}
if len(parent.Nodes) < 1 || parent.Nodes[0].Parent == nil {
return "", false
}
return findHtag(parent)
}