kok202
[2019.03.08] Go lang (웹 크롤링, JSON)

2019. 3. 8. 16:17카테고리 없음

웹 크롤링

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    resp, _ := http.Get("https://www.daum.net/")
    bytes, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(bytes))
    resp.Body.Close()
}







JSON 데이터 가져오기

주의 * struct 안의 멤버 변수이름이 대문자로 시작해야지 Unmarshal이 된다.

marshal     : json -> byte[]

unmarshal : byte[] -> json


package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

// A People struct to map json of homepage 'starwars api'
type Peolpe struct {
    Name string `json:"name"`
    Height string `json:"height"`
    Mass string `json:"mass"`
    HairColor string `json:"hair_color"`
    SkinColor string `json:"skin_color"`
    EyeColor string `json:"eye_color"`
    BirthYear string `json:"birth_year"`
    Gender string `json:"gender"`
    Homeworld string `json:"homeworld"`
    Films []string `json:"films"`
    Species []string `json:"species"`
    Vehicles []string `json:"vehicles"`
    Starships []string `json:"starships"`
    Created string `json:"created"`
    Edited string `json:"edited"`
    Url string `json:"url"`
}

func main() {
    resp, _ := http.Get("https://swapi.co/api/people/1")
    respBody, _ := ioutil.ReadAll(resp.Body)

    var respJson Peolpe
    json.Unmarshal(respBody, &respJson)
    fmt.Println(respJson.Name)
}