41 lines
738 B
Go
41 lines
738 B
Go
package cmd
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
func findSrcInHtml(doc string) []string {
|
|
var results []string
|
|
reader := strings.NewReader(doc)
|
|
if document, err := html.Parse(reader); err == nil {
|
|
|
|
var parser func(*html.Node)
|
|
parser = func(n *html.Node) {
|
|
if n.Type == html.ElementNode && n.Data == "img" {
|
|
|
|
var imgSrcUrl string
|
|
|
|
for _, element := range n.Attr {
|
|
if element.Key == "src" {
|
|
imgSrcUrl = element.Val
|
|
log.Print("found image:" + imgSrcUrl)
|
|
results = append(results, imgSrcUrl)
|
|
}
|
|
}
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
parser(c)
|
|
}
|
|
|
|
}
|
|
parser(document)
|
|
} else {
|
|
log.Panicln("Parse html error", err)
|
|
}
|
|
return results
|
|
}
|