goland 正则爬取邮箱地址

180it 2021-10-03 AM 121℃ 0条
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "regexp"
)

var (
    reEmail = `[\w\.]+@\w+\.[a-z]{2,3}(\.[a-z]{2,3})?`
)

func HandleError(err error, when string) {
    if err != nil {
        fmt.Println(when, err)
        os.Exit(1)
    }
}

func main() {

    html := GetHtml("https://www.douban.com/group/topic/41562980/")

    //模拟邮箱数据
    html += "xiaoming@163.com\n"
    html += "aaa@126.com\n"
    html += "22223@qq.com\n"
    html += "x.badt@gmail.com\n"

    re := regexp.MustCompile(reEmail)
    AallString := re.FindAllStringSubmatch(html, -1)
    for _, x := range AallString {
        fmt.Println(x)
    }

}

func GetHtml(url string) string {
    resp, err := http.Get(url)
    HandleError(err, "http.Get")
    bytes, _ := ioutil.ReadAll(resp.Body)
    html := string(bytes)
    return html
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

goland 正则爬取邮箱地址