-3

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.

goggins
  • 33
  • 5

2 Answers2

1

The fmt.Println() includes '\n' at the end. ln means new line. You can use fmt.PrintF() to format output however you want, it doesn't force a new line, if this is your issue.

Alexsen
  • 136
  • 1
  • 6
  • Then it prints everything on the same line, I want it to be like this: Title | Price (new line) – goggins Jan 19 '23 at 10:51
  • @goggins Don't use `.println()` on the name and use it on the price. This way, the name won't push the next printing thing on a new line, but the price will force one. – Alexsen Jan 19 '23 at 11:03
0

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")
}
}
Sahil jangra
  • 121
  • 7