golang html编码转换

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"

    "golang.org/x/net/html/charset"

    "golang.org/x/text/encoding"
    "golang.org/x/text/encoding/unicode"
    "golang.org/x/text/transform"
)

func main() {
    resp, err := http.Get("http://www.chinanews.com/")
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        fmt.Println("StatusCode", resp.Status)
        return
    }
    r := bufio.NewReader(resp.Body)
    e := determineEncoding(r)
    newReader := transform.NewReader(resp.Body, e.NewDecoder())
    result, err := ioutil.ReadAll(newReader)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println(string(result))
}

func determineEncoding(r *bufio.Reader) encoding.Encoding {
    bytes, err := r.Peek(1024)
    if err != nil {
        log.Printf("Fetch error: %v", err)
        return unicode.UTF8
    }
    encode, _, _ := charset.DetermineEncoding(bytes, "")
    return encode
}

你可能感兴趣的:(golang html编码转换)