爬虫被想太多,把他当作一个模拟别人的请求响应便可了,全部呢go写爬虫关键是写请求
json
package main import ( "bytes" "encoding/json" "io" "io/ioutil" "net/http" "time" ) func Get(url string) string { client := &http.Client{Timeout: 5 * time.Second} // 超时时间:5秒 至关于咱们爬虫中的timeout参数 resp, err := client.Get(url) //发起请求 //resp, err := http.NewRequest("GET", url) 也能够这样写 post同理 //增长header选项 resp.Header.Add("Cookie", "xxxxxx") resp.Header.Add("User-Agent", "xxx") resp.Header.Add("X-Requested-With", "xxxx") //cookies就直接加在请求头中就行了 if err != nil { //请求返回的错误参数 panic(err) } defer resp.Body.Close() //请求成功对于请求提进行解析 var buffer [512]byte result := bytes.NewBuffer(nil) for { n, err := resp.Body.Read(buffer[0:]) result.Write(buffer[0:n]) if err != nil && err == io.EOF { break } else if err != nil { panic(err) } } return result.String() } func main(){ print(Get("http://www.baidu.com")) }
//其余地方就省略咯 func Post(url string, data interface{}, contentType string) string { client := &http.Client{Timeout: 5 * time.Second} jsonStr, _ := json.Marshal(data) resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr)) if err != nil { panic(err) } defer resp.Body.Close() result, _ := ioutil.ReadAll(resp.Body) return string(result) }