-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdynamic_nextpagebtn_test.go
More file actions
67 lines (62 loc) · 1.49 KB
/
Copy pathdynamic_nextpagebtn_test.go
File metadata and controls
67 lines (62 loc) · 1.49 KB
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
package service
import (
"context"
"fmt"
"github.com/Jeffail/gabs/v2"
"github.com/JohnnyTing/rabida/config"
"github.com/chromedp/chromedp"
_ "github.com/unionj-cloud/go-doudou/framework/http"
"testing"
)
func TestRabidaImplDynamicNextPageBtn_Crawl(t *testing.T) {
conf := config.LoadFromEnv()
fmt.Printf("%+v\n", conf)
rabi := NewRabida(conf)
job := Job{
Link: "https://www.sjz.gov.cn/col/1596014942837/index.html",
CssSelector: CssSelector{
Scope: `.nr ul li`,
Attrs: map[string]CssSelector{
"title": {
Css: "a:first-child",
Attr: "title",
},
"link": {
Css: "a:first-child",
Attr: "href",
},
"date": {
Css: "span.date",
},
},
},
PaginatorFunc: func(currentPageNo int) CssSelector {
return CssSelector{
Css: fmt.Sprintf(`.center #MinyooPage>a[title="当前在第%d页"]+a`, currentPageNo),
}
},
Limit: 3,
}
opts := []chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.NoSandbox,
}
if conf.Mode == "headless" {
opts = append(opts, chromedp.Headless)
}
err := rabi.Crawl(context.Background(), job, func(ret []interface{}, nextPageUrl string, currentPageNo int) bool {
for _, item := range ret {
fmt.Println(gabs.Wrap(item).StringIndent("", " "))
}
if currentPageNo >= job.Limit {
return true
}
return false
}, nil, []chromedp.Action{
chromedp.EmulateViewport(1777, 903, chromedp.EmulateLandscape),
})
if err != nil {
t.Error(fmt.Sprintf("%+v", err))
}
}