Call doc.Select("a[href]"), then read each anchor’s href and TextContent(). Requires Go 1.24 or later.
go get recursiveai.org/go/onehtmlpackage main import ( "fmt" "log" "strings" "recursiveai.org/go/onehtml" ) func main() { const html = `<nav> <a href="/docs">Read <b>the docs</b></a> <a href="#contact">Contact</a> <a>Coming soon</a> </nav>` doc, err := onehtml.Parse(strings.NewReader(html)) if err != nil { log.Fatal(err) } links, err := doc.Select("a[href]") if err != nil { log.Fatal(err) } for _, link := range links { href, _ := link.Get("href") // Guaranteed by [href]. text := strings.TrimSpace(link.TextContent()) fmt.Printf("%s → %s\n", text, href) } }
Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onehtml, then go run .; or unzip the example and go run ..
Read the docs → /docs Contact → #contact
An anchor without href is skipped. The extracted destinations are not resolved or fetched. The example stops on a parse or selector error; a document with no matching links prints nothing.
Variations
For absolute URLs, add "net/url" to the imports. Before the loop, parse the page URL, then replace the loop with this version (links is the same slice):
base, err := url.Parse("https://example.com/guide") if err != nil { log.Fatal(err) } for _, link := range links { href, _ := link.Get("href") target, err := base.Parse(href) if err != nil { log.Printf("skip %q: %v", href, err) continue } text := strings.TrimSpace(link.TextContent()) fmt.Printf("%s → %s\n", text, target) }
Read the docs → https://example.com/docs Contact → https://example.com/guide#contact
Related recipes
The short answer
Query a[href], call Get("href") for the destination and TextContent() for the anchor’s text, and keep the two together. The destination is the parsed attribute value, not an automatically expanded URL. Resolve it separately if you need absolute links.
How do I extract all links from a webpage in Go?
First get the HTML you want to inspect, then parse it and select a[href]. That selector matches <a> elements with an href attribute, not <link> metadata, image sources, or <a> placeholders without a destination. If you only need links in navigation, narrow the selector to nav a[href]. With the document-wide selector, you get one match per anchor in document order, including separate anchors that lead to the same destination. Keep the duplicates if you’re auditing where links appear; collapse them only if you’re building a list of destinations.
The parser doesn’t request the page, follow the links, or run scripts that would construct anchors after load. Use a file, an HTTP response body, or already rendered HTML as input, depending on where the links you need are created. If href is present but empty, the selector still matches; skip empty values explicitly if your link list should exclude them.
How can I extract link text from HTML?
Use TextContent() on the matched anchor. It concatenates descendant text, so the b element in the sample contributes “the docs” without contributing its tags. strings.TrimSpace removes whitespace at the ends, not between words. The result is DOM text, not a measurement of what a browser displays or the accessible name for the link. For example, an image-only anchor can have no descendant text even when its image has an alt attribute. Don’t interpret an empty string from TextContent() as proof that the anchor has no accessible label.
How do relative URLs work when extracting links?
An href like /docs is relative to the site root, while #contact stays on the same page and changes the fragment. Other paths, such as next.html, are relative to the base URL’s directory. Use Go’s URL.Parse as in the variation rather than concatenating strings. An absolute reference is already independent of the base.
Use the document’s effective base when you want links to resolve as they would in a browser. In HTML, a <base href> element can change that base; the variation deliberately uses a known page URL for markup without one. URL resolution does not check whether a target exists.