package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
)
var (
reLink = `<a[\s\S]+?href="(http[\s\S+?])"` //需要根据实际情况书写规则
)
func HandleError(err error, when string) {
if err != nil {
fmt.Println(when, err)
os.Exit(1)
}
}
func main() {
html := GetHtml("https://www.hao123.com")
//爬取超链接
re := regexp.MustCompile(reLink)
AallString := re.FindAllStringSubmatch(html, -1)
for _, x := range AallString {
fmt.Println(x[0])
}
}
func GetHtml(url string) string {
resp, err := http.Get(url)
HandleError(err, "http.Get")
bytes, _ := ioutil.ReadAll(resp.Body)
html := string(bytes)
return html
}
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询