#Go Colly, how to select a paragraph within a div

50 messages ยท Page 1 of 1 (latest)

vale kernel
#

I am trying to select a small piece of text within a paragraph but I am unable to get it working.
I tried finding a tutorial online but nothing helped so far.

Hope someone can shine a light on what might be the solution.

<div class="priceindicator__container indicator-block"><svg class="priceindicator__svg"><use xlink:href="/images/svg/svg.svg#icon-daypart-ceremony"></use></svg> <div class="priceindicator__description"><h4> TITLE_HERE </h4> <p> TEXT_HERE <a class="text--s s-hide m-hide" tabindex="0"> <span id="tooltip-wrapper"> <svg id="1" class="preload-fas-fa-question-circle"><use xlink:href="#preload-fas-fa-question-circle"></use></svg> </span> </a> </p> </div> <!----></div>

I want the piece of text that is within the "p" inside of the div with the "priceindicator__description" class. Hope someone can explain to me how to do it.

c.OnHTML(".priceindicator__description p", func(e *colly.HTMLElement) {
//a := e.DOM.Find("p")
//fmt.Println(a.Html())
// fmt.Println(e.ChildText("p"))
})

This is what I tried in various ways, but nothing is giving me the text within the P tag.

buoyant relic
#
package main

import (
    "fmt"

    "github.com/gocolly/colly"
)

func main() {
    c := colly.NewCollector()

    c.OnHTML(".priceindicator__description p", func(e *colly.HTMLElement) {
        fmt.Println(e.DOM.Text())
    })

    if err := c.Visit("https://dozn.net/colly.html"); err != nil {
        panic(err)
    }
}
#

You can use strings.TrimSpace() to remove the extra spaces if you have that in the original HTML as well

vale kernel
#

Ohhh you are a hero Dozn, been trying to get it working for a whole day lol. Thank you very much, will try it tonight and let you know!

buoyant relic
#

No problem ^_^

vale kernel
#

Ahw, it returns nothing ๐Ÿ˜ฆ

buoyant relic
#

It loads all of that using JavaScript

vale kernel
#

Ohhhh dang and colly doesn't handle that properly? that means I will need a headless option?

buoyant relic
#

Depends on how they do it, sec

#

Still looking...

vale kernel
#

No worries take your time, really appreciating your help!

paper valley
#

i've been using surf for things like this so if colly doesn't support it I highly recommend surf

buoyant relic
#

Yeah there's a few for using an actual browser, but you generally want to avoid using them since they're slow

vale kernel
#

Surf? oh good to know thanks Dr. Drip ๐Ÿ™‚ But yeah slow, I need to scrape a couple of thousand pages soooo.... goopturtle

paper valley
#

hahah fair enough

buoyant relic
#

Can't believe it's this hard to find the price rofl

#

Normally it's just an XHR request, or it's embedded in the HTML. Not in this case, so I'm scouring the JS

vale kernel
#

Hahaha, yeah I had a hard time too. I can use a headless browser but I figured it was my zero experience with Colly that was the issue ๐Ÿ˜„

buoyant relic
#

I've made that mistake a few times in the past where I didn't realize it was being loaded dynamically, so I know the feeling ๐Ÿ˜‰

buoyant relic
#

I think they just obfuscated everything so you can't read it =/ Don't think I'll find anything at this rate lol

vale kernel
#

hahahaha

#

No problem, thanks so much for your time and help ๐Ÿ™‚

#

Think ill just try out Surf as Dr. Drip suggested

buoyant relic
#

No problem, best of luck

vale kernel
#

Javascript websites are awesome but not when you want to crawl them ๐Ÿ˜„

buoyant relic
#

Can't wait for WASM too

#

Where you'll have to crawl through the assembly

#

shudders

#

Or rather, WASM+rendering using graphics instead of the DOM

vale kernel
#

hehehe, will be joyful days ๐Ÿคช

buoyant relic
#

Holy shit I figured it out

#
{
    "id": 3,
    "category_id": 17,
    "name": "Feest",
    "type": "number",
    "use_in_avg": 1,
    "default": 50,
    "min": 0,
    "max": 0,
    "question": "Wat is de minimale prijs van een trouwfeest met 50 gasten?",
    "description": "Ga hierbij uit van 4 uur feest met 50 personen, inclusief drank en minimale kosten voor zaalhuur.",
    "frontend_question": "",
    "metric": "p.p.",
    "priority": 0,
    "frontend_description": null,
    "icon": "icon-daypart-party",
    "factor": 50,
    "tag_id": 851,
    "profile_id": 9673,
    "avg": 1775,
    "tag_set": true
}
#

avg / factor = price

#

So this one's for 35,50

#

It's directly in the HTML

#
package main

import (
    "encoding/json"
    "fmt"

    "github.com/gocolly/colly"
)

type Items []struct {
    Name                string      `json:"name"`
    Question            string      `json:"question"`
    Description         string      `json:"description"`
    FrontendQuestion    string      `json:"frontend_question"`
    FrontendDescription interface{} `json:"frontend_description"`
    Factor              int         `json:"factor"`
    Avg                 int         `json:"avg"`
}

func main() {
    c := colly.NewCollector()

    c.OnHTML("w-profile-price-indicator", func(e *colly.HTMLElement) {
        items := Items{}
        priceIndicators := e.Attr(":price-indicators")
        if err := json.Unmarshal([]byte(priceIndicators), &items); err != nil {
            panic(err)
        }

        for _, item := range items {
            price := float64(item.Avg) / float64(item.Factor)
            fmt.Printf("%s: %.2f\n", item.Name, price)
        }
    })

    if err := c.Visit("https://www.theperfectwedding.nl/bedrijven/9673/on-the-rock"); err != nil {
        panic(err)
    }
}
#

Output: ```
Ceremonie: 12.00
Receptie: 10.00
Feest: 35.50
Diner: 64.00
Overnachten: 0.00

buoyant relic
#

Worst part is that's the first place I looked, but couldn't connect the values >_<

vale kernel
#

sorry wasn' t there, but OMG thats awesome! let me read it a bit haha

buoyant relic
#

np

vale kernel
#

ohhh wow super nice!!!

buoyant relic
#

Enjoy ^_^

vale kernel
#

HERO! ๐Ÿ˜„

vale kernel
#

Works perfect indeed, thank you so much!!! now just a couple more things and done hahaha. Don' t know why but I am having a hard time using Colly haha, not sure when to use what function et cetera ๐Ÿ˜„

#

strings.TrimSpace(content.Text()) thats nice ๐Ÿ˜„