Created
December 29, 2022 13:20
-
-
Save robsongomes/231f0d3ec2bfe72937438a1238a76fb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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