Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支援網路文章摘要 #32

Open
kkdai opened this issue Feb 13, 2023 · 1 comment
Open

支援網路文章摘要 #32

kkdai opened this issue Feb 13, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@kkdai
Copy link
Owner

kkdai commented Feb 13, 2023

給定一個網址
抓取該網址全部文字內容
透過 ChatGPT 給出摘要

@kkdai kkdai added the enhancement New feature or request label Feb 13, 2023
@kkdai kkdai self-assigned this Feb 13, 2023
@kkdai
Copy link
Owner Author

kkdai commented Feb 13, 2023

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant