Go小爬蟲測試
阿新 • • 發佈:2018-12-27
package main import ( "fmt" "io/ioutil" "log" "net/http" ) type ThreadItem struct { url string content string imgs []string } func httpGet(url string) (content string, statusCode int, err error) { var ( res *http.Response data []byte ) if res, err = http.Get(url); err != nil { statusCode = -100 return } defer res.Body.Close() if data, err = ioutil.ReadAll(res.Body); err != nil { statusCode = -200 return } statusCode = res.StatusCode content = string(data) return } func getIndex() (err error) { var ( m ThreadItem ) m.url = "http://www.4chan.org/" if m.content, _, err = httpGet(m.url); err != nil { log.Println(err) return } fmt.Println(m.content) return } func main() { getIndex() }