golang写一个简单的爬虫

package main
import(
"fmt"
"io/ioutil"
"net/http"
)
func gethtml(url string) (r *http.Response, e error){
        resp,err := http.Get(url)
        if err != nil {
        fmt.Print("error")
        }
        return resp,err
}
func main(){
        resp, _ :=gethtml("http://www.baidu.com")
        defer resp.Body.Close()
        fmt.Println(resp.StatusCode)
        if resp.StatusCode==http.StatusOK{
                body,err :=ioutil.ReadAll(resp.Body)
                if err !=nil{
                     fmt.Println(err)
             }
             fmt.Println(string(body))

  

猜你喜欢

转载自www.cnblogs.com/c-x-a/p/9141748.html