Skip to content

Instantly share code, notes, and snippets.

@robsongomes
Created December 29, 2022 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsongomes/231f0d3ec2bfe72937438a1238a76fb7 to your computer and use it in GitHub Desktop.
Save robsongomes/231f0d3ec2bfe72937438a1238a76fb7 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"golang.org/x/net/html"
)
// WEB Scraping com Go!
func main() {
// res, err := http.Get("https://www.google.com/")
// if err != nil {
// log.Fatal(err)
// }
// defer res.Body.Close()
// input := res.Body
input, _ := os.Open("index.html")
node, _ := html.Parse(input)
var acessa func(*html.Node)
acessa = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "a" {
// lê o conteúdo da tag atual
fmt.Print(n.FirstChild.Data + " -> ")
for _, a := range n.Attr {
if a.Key == "href" {
fmt.Println(a.Val)
}
}
}
for next := n.FirstChild; next != nil; next = next.NextSibling {
acessa(next)
}
}
acessa(node)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment