Home > database >  Access DOM with Go and get data
Access DOM with Go and get data

Time:07-24

I want to access the HTML document tags from a URL, for example, I have the following webpage:

https://example.com/

I want the inside content from the h1 tag, "Example Domain":

<h1>Example Domain</h1>

Same for the <p> tag:

<p> More information...</p>

And then create a struct using the values from different tags:

type Example struct {
   foo string
   bar string
}

Example.foo = *h1 tag content*
Example.bar = *p tag content*

Is this possible?

CodePudding user response:

I would personally use goquery for this:

// Request the HTML page.
res, err := http.Get("https://example.com/")
if err != nil {
    log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
    log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}

// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
    log.Fatal(err)
}

h1 := doc.Find("h1").First().Text()
p := doc.Find("p").First().Text()

type Example struct {
    foo string
    bar string
}

e := Example{ foo: h1, bar: p }
  • Related