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 }