Golang : How to extract links from web page ?
Golang's third party package goquery provides excellent tools to assist developers in building HTML parser/crawler. In this tutorial, we will learn how to use goquery to extract links from a web page. The code below will scan a part of the web page that is within the <div class=".nav-collapse ..." >
and <ul class=".nav..">
and extract the links.
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"strings"
)
func ScrapeLinks(url string) {
doc, err := goquery.NewDocument(url)
if err != nil {
panic(err)
}
// process this part :
// <div class="nav-collapse collapse navbar-responsive-collapse">
// <ul class="nav nav-pills">
// <li>
// <a href="/references">References</a>
// </li>
// <li>
// <a href="/tutorials">Tutorials</a>
// </li>
// </ul>
// </div>
doc.Find(".nav-collapse .nav").Each(func(i int, s *goquery.Selection) {
Title := strings.TrimSpace(s.Find("li").Text()) // https://www.socketloop.com/tutorials/trim-white-spaces-string-golang
// convert string to array
Fields := strings.Fields(Title)
// go deeper by 1 level to get the <a href=""></a>
doc.Find(".nav-collapse .nav a").Each(func(i int, s *goquery.Selection) {
Link, _ := s.Attr("href")
Link = url + Link
fmt.Printf("Title is [%s] and link is [%s]\n", Fields[i], Link)
})
})
}
func main() {
ScrapeLinks("https://socketloop.com")
}
Output :
Title is [References] and link is [https://socketloop.com/references]
Title is [Tutorials] and link is [https://socketloop.com/tutorials]
Bear in mind that goquery's doc.Find only look for first part of the <div class="nav-collapse collapse navbar-responsive-collapse">
.. i.e .. only
doc.Find(".nav-collapse") // will do.
in this tutorial case, because we want to dig 1 level deeper to <ul class="nav...">
we use this instead
doc.Find(".nav-collapse .nav")
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.4k Golang : Read directory content with os.Open
+6.8k Web : How to see your website from different countries?
+6k Golang : Process non-XML/JSON formatted ASCII text file example
+12.4k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+11.7k Golang : Find and draw contours with OpenCV example
+21.6k Golang : Use TLS version 1.2 and enforce server security configuration over client
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+17.3k Golang : Clone with pointer and modify value
+8k Golang : Metaprogramming example of wrapping a function
+22.8k Golang : Randomly pick an item from a slice/array example
+35k Golang : Strip slashes from string example
+13.7k Golang : Compress and decompress file with compress/flate example