This repository was archived by the owner on Mar 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
74 lines (60 loc) · 1.79 KB
/
main.go
File metadata and controls
74 lines (60 loc) · 1.79 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
68
69
70
71
72
73
74
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/chromedp/chromedp"
)
func check(err error) {
if err != nil {
panic(err)
}
}
type info struct {
cnt string
title string
desc string
}
func main() {
// Get an url path
path := "https://flows.nodered.org/search?type=node&sort=downloads&page="
// Have to stdin total number of pages
var pages int
fmt.Scanln(&pages)
infos := make([]info, 0)
for i := 0; i < pages; i++ {
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
// Get HTML of node list
var html string
err := chromedp.Run(ctx,
chromedp.Navigate(path + fmt.Sprintf("%d", i + 1)),
chromedp.WaitVisible(`.gistbox-type`, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.InnerHTML(`.gistlist`, &html, chromedp.NodeVisible, chromedp.ByQuery),
)
check(err)
// Parce HTML
htmlReader := strings.NewReader(html)
doc, err := goquery.NewDocumentFromReader(htmlReader)
check(err)
// Find the review items
doc.Find(".gistbox-node").Each(func(cnt int, s *goquery.Selection) {
// For each item found, get the title
infos = append(infos, info{
cnt: fmt.Sprintf("%d-%d", i, cnt),
title: s.Find(".gisttitle").Text(),
desc: s.Find(".gistdescription").Text(),
})
})
}
fmt.Println("# List of nodes")
for _, el := range infos {
fmt.Printf("### %s: %s\n", el.cnt, el.title)
fmt.Printf("- %s\n", el.desc)
}
}