go 正则 爬取邮箱代码
package main import ( "net/http" "fmt" "io/ioutil" "regexp" ) var url string = "https://tieba.baidu.com/p/5518324938?red_tag=1795043739" var reEmail = `(\d+)@qq.com` func main() { resp, err := http.Get(url) handError(err) defer resp.Body.Close() bytesContent, err := ioutil.ReadAll(resp.Body) handError(err) getEmail(string(bytesContent)) } // 处理错误 func handError(err error) { //fmt.Println(err) if err != nil{ fmt.Println(err) } } // 获取邮箱 qq邮箱 使用正则 func getEmail(content string) { re := regexp.MustCompile(reEmail) list := re.FindAllStringSubmatch(content, -1) var result []string for _,v := range list { if v != nil{ result = append(result,v[0]) } } fmt.Println(result) }
结果
GOROOT=/usr/local/go #gosetup
GOPATH=/www/gopath #gosetup
/usr/local/go/bin/go build -i -o /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go /www/gopath/src/wang/main.go #gosetup
/private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go #gosetup
[3304778804@qq.com 742948861@qq.com 1261766424@qq.com 2903927557@qq.com 973327005@qq.com 973327005@qq.com 51061804@qq.com 3154089413@qq.com 2501667505@qq.com 1733510262@qq.com 289207537@qq.com 11886843@qq.com 1069016912@qq.com 765730856@qq.com 643074575@qq.com 330256148@qq.com 2025643306@qq.com 1903987556@qq.com 136772698@qq.com 990242520@qq.com 876958445@qq.com 776283765@qq.com 184756372@qq.com 2580145919@qq.com 710830708@qq.com 459618331@qq.com 964974049@qq.com 1499820286@qq.com 1822036045@qq.com 29522755@qq.com 1903987556@qq.com 2444694413@qq.com]
Process finished with exit code 0
package main import ( "net/http" "fmt" "io/ioutil" "regexp" ) var url string = "https://tieba.baidu.com/p/5934645800" var reQQEmail = `(\d+)@qq.com` var reEmail = `\w+@\w+\.\w+` func main() { content := getPageString(url) res1 := getEmail(content,reEmail) res2 := getEmail(content,reQQEmail) fmt.Println("一共找到邮箱",len(res1)) fmt.Println("一共找到邮箱",len(res2)) } // 处理错误 func handError(err error) { //fmt.Println(err) if err != nil{ fmt.Println(err) } } // 获取邮箱 qq邮箱 使用正则 func getEmail(content string,reg string) []string { re := regexp.MustCompile(reg) list := re.FindAllStringSubmatch(content, -1) var result []string for _,v := range list { if v != nil{ result = append(result,v[0]) } } return result } func getPageString(url string) string { resp, err := http.Get(url) handError(err) defer resp.Body.Close() bytesContent, err := ioutil.ReadAll(resp.Body) handError(err) return string(bytesContent) }