January 2019
Beginner
132 pages
3h 14m
English
One quick and easy piece of information that we could extract using the strings package is to count the number of links that are contained in a web page. The strings package has a function called Count(), which returns the number of times a substring occurs in a string. As we have seen before, links are contained in <a> tags. By counting the number of occurrences of "<a", we can get a general idea of the number of links in a page. An example would look like the one given here:
package mainimport ( "fmt" "io/ioutil" "net/http" "strings")func main() { resp, err := http.Get("https://www.packtpub.com/") if err != nil { panic(err) } data, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } stringBody := string(data) ...Read now
Unlock full access