版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15977699/article/details/87880524
安装golang.org/x/net/html、golang.org/x/text
在gopath的src目录下创建golang.org/x目录,进入到golang.org/x目录,执行命令:
git clone https://github.com/golang/net.git
git clone https://github.com/golang/text.git
具体实现
package main
import (
"bufio"
"fmt"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"net/http"
)
func main() {
resp, err := http.Get("http://www.zhenai.com/zhenghun")
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("Error status code:", resp.StatusCode)
return
}
// 编码转换
htmlEncoding:=determineEncoding(resp.Body)
reader := transform.NewReader(resp.Body, htmlEncoding.NewDecoder())
body, err := ioutil.ReadAll(reader)
//body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("%s \n", body)
}
// 判断html charset 并返回encoding.Encoding
func determineEncoding(r io.Reader) encoding.Encoding {
bytes, err := bufio.NewReader(r).Peek(1024)
if err != nil {
panic(err)
}
e, _, _ := charset.DetermineEncoding(bytes, "")
return e
}