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
|
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"time"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
type Link struct {
Href string
Text string
}
func main() {
// Logging configuration.
log.SetFlags(log.LstdFlags | log.Lshortfile)
// CLI flag configuration.
rawURL := flag.String("url", "", "Web address of target HTML")
timeoutSecs := flag.Int("timeout", 2, "Number of seconds after which to time out")
flag.Parse()
if *rawURL == "" {
log.Fatal("Missing -url")
}
// Configure the request.
timeout := time.Duration(*timeoutSecs) * time.Second
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest(http.MethodGet, *rawURL, nil)
if err != nil {
log.Fatal(err)
}
// Perform the request.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
links, err := findLinks(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(links)
}
// findLinks consumes the given [io.Reader], scraping it of anchor
// tags. Each anchor tag is "unmarshalled" into a [Link]. The
// resulting slice of Links is returned, along with an error.
func findLinks(r io.Reader) ([]Link, error) {
doc, err := html.Parse(r)
if err != nil {
return nil, fmt.Errorf("can't parse html reader: %w", err)
}
var links []Link
for n := range doc.Descendants() {
if n.Type == html.ElementNode && n.DataAtom == atom.A {
var link Link
// Scan the href.
for _, a := range n.Attr {
if a.Key == "href" {
link.Href = a.Val
}
}
// FIXME: for now, only scan for hrefs.
links = append(links, link)
}
}
return links, nil
}
|