Search code examples
jquerygogoquery

What am I doing wrong parsing HTML with Goquery using a class selector?


I'm using goquery to get some data from some sites. I have no problem when the element has an id, but it seems I can't make it work when the element only has a class.

For example, assume this site. I want to retrieve the name and the price. To do so, I'm using:

func fetch(doc *goquery.Document) (name string, price string) {
    name = doc.Find(".main-info__title-main").Text()
    if name == "" {
        log.Fatal("Could not retrieve property name")
    }
    price = doc.Find(".info-data-price").Text()
    if price == "" {
        log.Fatal("Could not retrieve property price")
    }
    return name, price
}

However, I'm both cases the content is always "". If instead of class, the span had an id, it would works without any issues, changing . for #, i.e. doc.Find("#main-info__title-main").Text(). So, what am I doing wrong here, using the selector by class name?


Solution

  • It turns out the code was actually ok. Further debugging I found that the site wasn't correctly fetched due to CAPTCHA.