Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Created July 25, 2022 16:13
Show Gist options
  • Save manjeettahkur/e5e5e9618e63f02aea1dd0a32a8ecba3 to your computer and use it in GitHub Desktop.
Save manjeettahkur/e5e5e9618e63f02aea1dd0a32a8ecba3 to your computer and use it in GitHub Desktop.
parse data
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/fatih/color"
"golang.org/x/net/html"
)
func main() {
fmt.Println("welcome to Golang World!")
getHeaders()
}
func getHeaders() {
var urls []string
urls = append(urls, "https://microsoft.com/")
for i := range urls {
resp, err := http.Get(urls[i])
if err != nil {
log.Fatalln(err)
}
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
body := string(responseBody)
reader := strings.NewReader(body)
tokenizer := html.NewTokenizer(reader)
for {
tt := tokenizer.Next()
switch {
case tt == html.ErrorToken:
if tokenizer.Err() == io.EOF {
color.Green("End of file, we done!")
return
}
color.Red(tokenizer.Err().Error())
case tt == html.StartTagToken:
t := tokenizer.Token()
if t.Data == "h1" {
fmt.Println("h1 tag found")
color.Blue(t.String())
}
case tt == html.TextToken:
t := tokenizer.Token()
color.Yellow(t.String())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment