My Learning Blog

Some small projects in golang - 10/23/2024

coding in go

To parse an HTML file to count the number of words and pictures:

package main

import (
	"fmt"
	"golang.org/x/net/html"
	"io"
	"net/http"
	"strings"
)

// Function to count words and images in the HTML
func countWordsAndImages(r io.Reader) (int, int) {
	doc, err := html.Parse(r)
	if err != nil {
		fmt.Println("Error parsing HTML:", err)
		return 0, 0
	}

	var wordCount,imageCount int
  dfs(doc, &wordCount, &imageCpunt)
  return wordCount,inmageCount
}

func dfs(*html.Node, *wordCount,*imageCount ){

		if n.Type == html.TextNode {
			words := strings.Fields(n.Data)
			wordCount += len(words)
		}
		if n.Type == html.ElementNode && n.Data == "img" {
			imageCount++
		}
		// Recursively traverse the HTML nodes
		for c := n.FirstChild; c != nil; c = c.NextSibling {
      dfs(c,wordsCount,imageCount)
		}

}

func main() {
	// Example: Fetch an HTML page (or you can parse from a file)
	resp, err := http.Get("https://example.com")
	if err != nil {
		fmt.Println("Error fetching page:", err)
		return
	}
  // The caller must close the response body when finished with it:
	defer resp.Body.Close()

	// Count words and images in the page
	wordCount, imageCount := countWordsAndImages(resp.Body)

	fmt.Printf("Word Count: %d\n", wordCount)
	fmt.Printf("Image Count: %d\n", imageCount)
}