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

栏目: Go · 发布时间: 7年前

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乱码的问题?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Foundations of PEAR

Foundations of PEAR

Good, Nathan A./ Kent, Allan / Springer-Verlag New York Inc / 2006-11 / $ 50.84

PEAR, the PHP Extension and Application Repository, is a bountiful resource for any PHP developer. Within its confines lie the tools that you need to do your job more quickly and efficiently. You need......一起来看看 《Foundations of PEAR》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具