目录
背景
http.Get(url)

原因分析
http.Get(url)http.NewRequest
代码部分
func main7() {
client := &http.Client{}
url := "https://movie.douban.com/top250?start=0&filter="
reqest, err := http.NewRequest("GET", url, nil)
//设置header属性
reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 OPR/66.0.3515.115")
if err!=nil {
fmt.Println(err)
return
}
response, _ := client.Do(reqest)
defer response.Body.Close()
buf := make([]byte,4096)
var result string
for {
n,err:=response.Body.Read(buf)
if n == 0 {
fmt.Println("读取网页完成")
break
}
if err!=nil && err!=io.EOF {
fmt.Println("resp body err",err)
return
}
result += string(buf[:n])
//打印读取的网页
fmt.Println(result)
}
}
您可能感兴趣的文章: