forked from badoux/goscraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goquery_test.go
132 lines (121 loc) · 3.83 KB
/
goquery_test.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
// 2017/12/30 17:37:14 Sat
package goscraper
import (
"fmt"
"strings"
"testing"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/hms58/dateparse"
"github.com/hms58/goscraper"
)
func TestGoQueryFile(t *testing.T) {
htmlFile := "test/jianshu.html"
s, err := goscraper.Scrape(&goscraper.Options{
Url: "https://www.jianshu.com/p/fa24238d84a9",
HtmlFile: htmlFile,
Handler: &goscraper.DefaultHandler{},
})
if err != nil {
fmt.Println(err)
return
}
// fmt.Printf("Title : %s\n", s.Preview.Title)
// fmt.Printf("Description : %s\n", s.Preview.Description)
// fmt.Printf("Image: %s\n", s.Preview.Images[0])
fmt.Printf("Image: %d\n%v\n", len(s.Preview.Images), s.Preview.Images)
fmt.Printf("RawImage: %d\n%v\n", len(s.Preview.RawImages), s.Preview.RawImages)
fmt.Printf("RawScripts: %d\n%v\n", len(s.Preview.RawScripts), s.Preview.RawScripts)
// fmt.Printf("Url : %s\n", s.Preview.Link)
//
fmt.Printf("title: %s\n", s.Preview.Title)
fmt.Printf("body: %d\n", CountTextWords(s.Find("div[id=\"js_content\"]").Eq(0).Text()))
fmt.Printf("body: %d\n", strings.Count(s.Find("div[id=\"js_content\"]").Eq(0).Text(), ""))
publishTimeStr := s.Find("em[id=\"post-date\"]").Eq(0).Text()
// publishTime, _ := time.Parse("2017-12-13", publishTimeStr)
publishTime, _ := dateparse.ParseIn("2017-12-13", time.Local)
// publishTime2, _ := dateparse.ParseAny("2017-12-13")
fmt.Printf("publish_time: %s, %s, %s\n", publishTimeStr, publishTime.String())
var topics []string
s.Find("div[id=\"js_content\"]").Find("a").Each(func(index int, sel *goquery.Selection) {
if val, ok := sel.Attr("href"); ok {
fmt.Printf("title: %s, %s\n", sel.Text(), val)
if isWechatTopic(val) {
val = strings.Replace(val, "&", "&", -1)
topics = append(topics, val)
}
}
})
fmt.Printf("topics %d\n %v", len(topics), topics)
var ahrefs []string
s.Find("a").Each(func(index int, sel *goquery.Selection) {
if val, ok := sel.Attr("href"); ok {
// fmt.Printf("title: %s, %s\n", sel.Text(), val)
if goscraper.IsImageUrl(val) {
ahrefs = append(ahrefs, val)
}
}
})
fmt.Printf("a hrefs: %d\n%v\n", len(ahrefs), ahrefs)
}
func isWechatTopic(s string) bool {
if strings.HasPrefix(s, "https://mp.weixin.qq.com/s?__biz=") ||
strings.HasPrefix(s, "http://mp.weixin.qq.com/s?__biz=") {
return true
}
return false
}
func TestGoqueryBody(t *testing.T) {
body := `
<html>
<head>
<link href="//mmbiz.qpic.cn" />
<link href="https://mmbiz.qpic.cn/test/hello" />
<link href="https://mmbiz.qpic.cn/test/good.css" />
</head>
<body>
<img src="http://test.com/test.png?x=y"/>
<img src="//test.com/test.png?x=y"/>
<img src="/test.com/test.png?x=y"/>
<img src="warning/test.png?x=y"/>
<img src=""/>
test
</body>
</html>
`
s, err := goscraper.Scrape(&goscraper.Options{
Url: "http://www.jianshu.com/p/99b7f266a7ec",
Body: body,
Handler: &goscraper.DefaultHandler{},
})
if err != nil {
fmt.Println(err)
return
}
// fmt.Printf("Title : %s\n", s.Preview.Title)
// fmt.Printf("Description : %s\n", s.Preview.Description)
// fmt.Printf("Image: %s\n", s.Preview.Images[0])
fmt.Printf("Image: %d\n%v\n", len(s.Preview.Images), s.Preview.Images)
fmt.Printf("RawImage: %d\n%v\n", len(s.Preview.RawImages), s.Preview.RawImages)
fmt.Printf("Js: %d\n%v\n", len(s.Preview.Scripts), s.Preview.Scripts)
fmt.Printf("RawJs: %d\n%v\n", len(s.Preview.RawScripts), s.Preview.RawScripts)
// fmt.Printf("Url : %s\n", s.Preview.Link)
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
text := s.Find("body").Eq(0).Text()
fmt.Printf("body: %d\n", CountTextWords(text))
}
func CountTextWords(text string) int {
skipCountLetter := "\r\n "
count := 0
for _, word := range text {
// fmt.Printf("----%c----", word)
if !strings.ContainsRune(skipCountLetter, word) {
count = count + 1
}
}
return count
}