Web Scraping Code error

Hello GoLang World!

This is my very first post. { (I am sure more to come :grin:) }

Following a video tutorial, I typed in my code, but getting two errors so far can’t figure out how or what is needed to resolve those two errors.

Using Visual Studio Code on windows 10.

1= .\main.go:25:4: undefined: log
collector.OnHTML(".factList li", func(element *colly.HTMLElement){
factID, err := strconv.Atoi(element.Attr(“id”))
if err != nil {
log.Println(“Could not get id”)

2= .\main.go:31:8: undefined: factId
factDesc := element.Text

	fact := Fact{
		ID: factId,
		Description: factDesc,
	}

Here is the complete lines of code

package main

import (

"github.com/gocolly/colly"

"strconv"

"fmt"

)

type Fact struct {

ID          int    `json:"id"`

Description string `json:"description"`

}

func main() {

allFacts := make([]Fact, 0)

collector := colly.NewCollector(

    colly.AllowedDomains("factretriever.com", "www.factretreiver.com"),

)

collector.OnHTML(".factList li", func(element *colly.HTMLElement){

    factID, err := strconv.Atoi(element.Attr("id"))

    if err != nil {

        log.Println("Could not get id")

    }

    factDesc := element.Text

    fact := Fact{

        ID: factId,

        Description: factDesc,

    }

    allFacts = append(allFacts, fact)

    

})

collector.OnRequest(func(request *colly.Request) {

    fmt.Println("Visiting: ", request.URL.String())

})

collector.Visit("https://www.factretriever.com/rhino-facts")

}

Hi,
For error 1, seems like you forgot to import log package so, it would be:

import (
"github.com/gocolly/colly"
"strconv"
"fmt"
"log"
)

And for error 2, there is a small typo as you declared:

 factID, err := strconv.Atoi(element.Attr("id"))

and later used it as:

fact := Fact{
        ID: factId,
        Description: factDesc,

    }

So it should be factID instead of factId