Open kkdai opened 1 year ago
refer code
package main
import (
"fmt"
"net/http"
"golang.org/x/net/html"
"strings"
)
func main() {
url := "https://www.example.com"
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error retrieving the URL:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("Error status code:", resp.StatusCode)
return
}
doc, err := html.Parse(resp.Body)
if err != nil {
fmt.Println("Error parsing HTML:", err)
return
}
var textContent func(*html.Node)
textContent = func(n *html.Node) {
if n.Type == html.TextNode {
text := strings.TrimSpace(n.Data)
if text != "" {
fmt.Println(text)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
textContent(c)
}
}
textContent(doc)
}
給定一個網址 抓取該網址全部文字內容 透過 ChatGPT 給出摘要