func findHrefWithPrefix(n *html.Node, prefix string, results map[string]*html.Node) {
if n.Type == html.ElementNode && n.Data == "a" {
for _, attr := range n.Attr {
if attr.Key == "href" && strings.HasPrefix(attr.Val, prefix) {
// 如果节点还没有被添加到结果集中,则添加它
if results[attr.Val] == nil {
results[attr.Val] = n
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findHrefWithPrefix(c, prefix, results)
}
}
prefix参数,可供获取链接地址是指定前缀的a标签节点。


