-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (68 loc) · 1.48 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func ExampleScrape() {
res, err := http.Get("https://stackoverflow.com/questions/tagged/javascript")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
doc.Find(".question-summary .summary").Each(func(i int, s *goquery.Selection) {
title := s.Find("H3").Text()
fmt.Println(i, title)
})
}
type Earthquake struct {
Timestamp string
Latitide string
Longitude string
Magnitudo string
Depth string
Territory string
}
var arrayEarthquake []Earthquake
func postScrape() {
doc, err := goquery.NewDocument("http://www.bmkg.go.id/gempabumi/gempabumi-terkini.bmkg")
if err != nil {
log.Fatal(err)
}
doc.Find("tbody tr").Each(func(_ int, tr *goquery.Selection) {
e := Earthquake{}
tr.Find("td").Each(func(ix int, td *goquery.Selection) {
switch ix {
case 1:
e.Timestamp = td.Text()
case 2:
e.Latitide = td.Text()
case 3:
e.Longitude = td.Text()
case 4:
e.Magnitudo = td.Text()
case 5:
e.Depth = td.Text()
case 6:
e.Territory = td.Text()
}
})
arrayEarthquake = append(arrayEarthquake, e)
})
fmt.Println(arrayEarthquake)
}
func main() {
fmt.Print("ExampleScrape: \n")
ExampleScrape()
fmt.Print("\n")
fmt.Print("postScrape: \n")
postScrape()
}