O
O
OctorberMyMonth2021-03-23 20:12:52
go
OctorberMyMonth, 2021-03-23 20:12:52

How to display Titles and Links on the same line?

package main

import (
  "fmt"
  "log"

  "github.com/PuerkitoBio/goquery"
)

type Cyberport struct {
  Titles []string
  Links  []string
}

func main() {
  var cyber Cyberport
         // получаем ответ на запрос 
  document, err := goquery.NewDocument("https://www.cybersport.ru/news")
  if err != nil {
    log.Fatal(err)
  }
        // получаем текст статьи
  document.Find(".cs-news__link").Each(func(index int, item *goquery.Selection) {
    text := item.Text()
    cyber.Titles = append(cyber.Titles, text)
  })
        // получаем ссылку на новость
  document.Find(".cs-news__text").Each(func(index int, item *goquery.Selection) {
    linkTag := item.Find("a")
    link, _ := linkTag.Attr("href")
    cyber.Links = append(cyber.Links, link)
  })
        // выводим содержимое
  for index, item := range cyber.Links {
    fmt.Printf("%d: %v\n", index, item)
  }
}

I need the title of the Article and the link to be displayed in the terminal, (example, Sunlight: "Techies pickers are sick people" : https://www.lalal.com )

Thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Samsonov, 2021-03-23
@OctorberMyMonth

for index, item := range cyber.Links {
  fmt.Printf("%s: %v\n", cyber.Titles[index], item)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question