.Maui Blog

Golang – Find all links in a webpage

At dotmaui.com we use Go (Golang), the programming language of Google, more and more often. The dashboard is in fact based on Revel, a high-productivity web framework for the Go language,  and other automation scripts we have created are made with this fantastic language.

Today, we wanted to share an example with you using Goquery, that brings a syntax and a set of features similar to jQuery to the Go language.
Let’s see how to retrieve all links from a web page.

package main

import (
    "fmt"        
    "log"
    "github.com/PuerkitoBio/goquery"
)

func main() {

    doc, err := goquery.NewDocument("https://en.wikipedia.org/wiki/Example.com")
    
    if err != nil {
        log.Fatal(err)
    }
    
    doc.Find("a[href]").Each(func(index int, item *goquery.Selection) {
        href, _ := item.Attr("href")
        fmt.Printf("link: %s - anchor text: %s\n", href, item.Text())
        
    })
    
}
Exit mobile version