[Golang] Fetch DOM Style Object Properties via goquery


Fetch properties of HTML DOM Style Object via goquery - example of Golang web scraping.

The idea of web scraping is not difficult if the webpage is not rendered by JavaScript. View source of the webpage, find out where the data is, and apply correct CSS selector to extract the HTML element which contains the data we need. The following Go code fetches the properties of HTML DOM Style Object in W3Schools.

package codegen

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

func GetAllStyleProperty() (cssprops []string, err error) {
      url := "https://www.w3schools.com/jsref/dom_obj_style.asp"

      doc, err := goquery.NewDocument(url)
      if err != nil {
              return
      }

      csspropslinks := doc.Find(".w3-table-all").First().Find("a")
      csspropslinks.Each(func(i int, s *goquery.Selection) {
              cssprops = append(cssprops, s.Text())
      })

      return
}

Print the fetched properties on screen.

package codegen

import (
      "testing"
)

func TestGetAllStyleProperty(t *testing.T) {
      cssprops, err := GetAllStyleProperty()
      if err != nil {
              t.Error(err)
      }

      for _, prop := range cssprops {
              println(prop)
      }
}

Tested on: Ubuntu Linux 16.10, Go 1.8.


References:

[1]GitHub - PuerkitoBio/goquery: A little like that j-thing, only in Go. godoc
[2]Tips and tricks · PuerkitoBio/goquery Wiki · GitHub
[3]HTML DOM Style object - W3Schools