go爬虫并解决中文乱码
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/axgle/mahonia"
)
func main() {
url := "http://www.baidu.com"
// 生成client客户端
client := &http.Client{}
//生成Request对象
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
}
//发起请求
resp, err := client.Do(req)
//关闭响应体
defer resp.Body.Close()
// 读取响应
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
//解决中文乱码
bodystr := mahonia.NewDecoder("gbk").ConvertString(string(body))
fmt.Println(bodystr)
}
网友评论