-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrappingDataFromWeb.go
55 lines (47 loc) · 1.07 KB
/
ScrappingDataFromWeb.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
// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const urlToBeScrapped = "http://services.explorecalifornia.org/json/tours.php"
func main() {
fmt.Printf("Starting to read a file from Internet\n")
resp, err := http.Get(urlToBeScrapped)
checkError(err)
fmt.Printf("respons Type received is %T\n", resp)
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
checkError(err)
content := string(bytes)
//fmt.Println("Content scrapped from the page is: ", content)
tours := readToursFromRawData(content)
for _, val := range tours {
fmt.Println(val.Name)
}
}
func readToursFromRawData(content string) []Tour {
tours := make([]Tour, 0, 20)
decoder := json.NewDecoder(strings.NewReader(content))
_, err := decoder.Token()
checkError(err)
var tour Tour
for decoder.More() {
err := decoder.Decode(&tour)
checkError(err)
tours = append(tours, tour)
}
return tours
}
type Tour struct {
Price, Name string
}
func checkError(err error) {
if err != nil {
panic(err)
}
}