2019-02-19 00:23:44 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/html"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func htmlTitleRecurse(h *html.Node) string {
|
2019-03-10 21:15:48 +08:00
|
|
|
if h == nil || h.FirstChild == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:23:44 +08:00
|
|
|
if h.Type == html.ElementNode && h.Data == "title" {
|
|
|
|
return h.FirstChild.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
for c := h.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
res := htmlTitleRecurse(c)
|
|
|
|
if res != "" {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func htmlTitleGet(url string) (string, error) {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
h, err := html.Parse(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlTitleRecurse(h), nil
|
|
|
|
}
|