package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.Get("http://www.baidu.com") if err != nil { fmt.Println("http get error.") } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("http read error.") } src := string(body) fmt.Println(src) }
抓取并进行简单的匹配过滤
package main import ( "fmt" "io/ioutil" "net/http" "regexp" ) func main() { resp, err := http.Get("http://www.baidu.com") if err != nil { fmt.Println("http get error.") } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("http read error.") } src := string(body) re, _ := regexp.Compile("关于.{1,2}") src = re.FindString(src) fmt.Println(src) }
有疑问加站长微信联系(非本文作者)