Magic go language (web download)
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Currently, there are a lot of web page crawling code on the Internet. However, after reading the go language web download code, I found that its web page download code is the simplest. If you don't believe it, you can check it out,
package main import( "fmt" "log" "net/http" "os") func main(){ resp,err:=http.Get("http://www.baidu.com") if err!=nil{ //handleerror fmt.Println(err) log.Fatal(err) } defer resp.Body.Close() if resp.StatusCode==http.StatusOK{ fmt.Println(resp.StatusCode) } buf:=make([]byte,1024) //createfile f,err1:=os.OpenFile("baidu.html",os.O_RDWR|os.O_CREATE|os.O_APPEND,os.ModePerm) if err1!=nil{ panic(err1) return } defer f.Close() for{ n,_:=resp.Body.Read(buf) if 0==n{ break } f.WriteString(string(buf[:n])) }}
You can pay attention to these functions, namely http. Get, OS. OpenFile, resp. Body. Read, f. WriteString. Imagine what these functions are used. As their names are described, their functions are http download, file creation, character reading, and file writing. No. If you are interested, you can copy and test the code.
If I remember correctly, this part of code is referenced elsewhere. You are welcome to contact me. I will comment on it later.