main1.go 802 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/PuerkitoBio/goquery"
  6. )
  7. func main() {
  8. top, err := getTop(5)
  9. if err != nil {
  10. panic(err)
  11. }
  12. fmt.Printf("%v", top)
  13. }
  14. func getTop(n int) ([]string, error) {
  15. top := make([]string, n)
  16. doc, err := goquery.NewDocument("http://dctf.defcamp.ro/ranks")
  17. if err != nil {
  18. return nil, err
  19. }
  20. rec := make(chan string)
  21. go func() {
  22. doc.Find(".table-hover > tbody > tr").Each(func(i int, s *goquery.Selection) {
  23. if i > n-1 {
  24. return
  25. }
  26. var (
  27. team = strings.TrimSpace(s.Find(".fa-users").Parent().Text())
  28. points = strings.TrimSpace(s.Find(".fa-flag").Parent().Text())
  29. )
  30. rec <- fmt.Sprintf("#%2d %6s %s\n", i+1, points, team)
  31. })
  32. }()
  33. for idx := 0; idx < n; idx++ {
  34. top[idx] = <-rec
  35. }
  36. close(rec)
  37. return top, nil
  38. }