-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (51 loc) · 1.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/PuerkitoBio/goquery"
"io"
"log"
"net/http"
"os"
"strings"
)
func ParseWiki(url string) []string {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
// Create array for tracks
var tracks []string
var track string
tracks = append(tracks, "artist,song\n")
// Find all songs on page and parse string into artist and song
doc.Find(".div-col").Each(func(_ int, s *goquery.Selection) {
s.Find("li").Each(func(_ int, t *goquery.Selection) {
text := strings.Split(t.Text(), " –")
artist := text[0]
song := strings.Trim(text[1], " \"")
// Create track
track = artist + "," + song + "\n"
tracks = append(tracks, track)
})
})
return tracks
}
func main() {
url := "https://en.wikipedia.org/wiki/The_Pitchfork_500"
out_filename := "tracks.csv"
tracks := ParseWiki(url)
file, _ := os.Create(out_filename)
defer file.Close()
var err error
for _, track := range tracks {
_, err = io.WriteString(file, track)
if err != nil {
log.Fatal(err)
}
file.Sync()
}
}