Search code examples
goweb-scrapinggo-colly

Printing 2 statements on the same line


I've successfully made a scraper that scrapes all 109 pages of the iPhone section on eBay.

The problem is that I need them to print on the same line. This is what it currently looks likepicture

package main

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

func main() {
    c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"))

    c.OnHTML(".s-item__title", func(element *colly.HTMLElement) {
        element.ChildAttr("heading", "role")
        fmt.Println(element.Text)
    })

    c.OnHTML(".s-item__price", func(element *colly.HTMLElement) {
        fmt.Println(element.Text)
    })

    c.Visit("https://www.ebay.com/sch/i.html?_from=R40&_nkw=iPhone&_sacat=0&_pgn=1")
}

It's not even possible to navigate around this information. Can someone show me how I can get the Title along with the price on the same line?

I thought about renaming the element but it didn't work.

I would use printf or println, but then it just prints everything together.


Solution

  • Try using fmt.Print instead of fmt.Println

    package main
    
    import "fmt"
    
    func main() {
    {
        fmt.Print("Title")
    }
    {
        fmt.Println("| Price")
    }
    
    {
        fmt.Print("Title")
    }
    {
        fmt.Println("| Price")
    }
    }