美文网首页
golang学习笔记之-采集gbk乱码的问题?

golang学习笔记之-采集gbk乱码的问题?

作者: Maggie_up | 来源:发表于2019-02-18 18:46 被阅读0次
    package main
    
    import (
        "bufio"
        "fmt"
        "golang.org/x/net/html/charset"
        "golang.org/x/text/transform"
        "io"
        "io/ioutil"
        "net/http"
    )
    
    func DetermineEncoding(r io.Reader) []byte {
        OldReader := bufio.NewReader(r)
        bytes, err := OldReader.Peek(1024)
        if err != nil {
            panic(err)
        }
        e, _, _ := charset.DetermineEncoding(bytes, "")
        reader := transform.NewReader(OldReader, e.NewDecoder())
        all, err := ioutil.ReadAll(reader)
        if err != nil {
            panic(err)
        }
        return all
    }
    
    func main() {
        resp, err := http.Get(`http://www.chinanews.com`)
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()
        fmt.Printf(`%s`, DetermineEncoding(resp.Body))
    }
    

    相关文章

      网友评论

          本文标题:golang学习笔记之-采集gbk乱码的问题?

          本文链接:https://www.haomeiwen.com/subject/asszeqtx.html