Why does this program not print anything?

hermancain :

I'm trying to use Go to parse html. I would like to print the html to the terminal and I don't understand why this doesn't print anything:

package main

import (
        "fmt"
        "log"
        "net/http"

        "golang.org/x/net/html"
)

func main() {
        r, err := http.Get("https://google.com")
        if err != nil {
                log.Panicln(err)
        }

        defer func() {
                err := r.Body.Close()
                if err != nil {
                        fmt.Println(err)
                }
        }()

        node, err := html.Parse(r.Body)
        if err != nil {
                log.Panicln(err)
        }
        fmt.Println(node.Data)
}

I know there are different ways to print the html, but I don't understand why this in particular never prints anything no matter what website I use. Is this intended behavior?

Docs:

https://godoc.org/golang.org/x/net/html#Node

https://github.com/golang/net/blob/master/html/node.go#L38

Igor Prozhoha :

Because it's a tree of the HTML. Upper level is empty. For example if you need parse all url from html:

package main

import (
        "fmt"
        "log"
        "net/http"

        "golang.org/x/net/html"
)

func main() {
        r, err := http.Get("https://google.com")
        if err != nil {
                log.Panicln(err)
        }

        defer func() {
                err := r.Body.Close()
                if err != nil {
                        fmt.Println(err)
                }
        }()

        node, err := html.Parse(r.Body)
        if err != nil {
                log.Panicln(err)
        }
        fmt.Println(node.Data)

        var f func(*html.Node)
        f = func(n *html.Node) {
            if n.Type == html.ElementNode && n.Data == "a" {
                for _, a := range n.Attr {
                    if a.Key == "href" {
                        fmt.Println(a.Val)
                        break
                    }
                }
            }
            for c := n.FirstChild; c != nil; c = c.NextSibling {
                f(c)
            }
        }
        f(node)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this code not print anything?

Why does my char array not print anything?

Why does this threaded code not print anything?

Why does this Python code not print anything

Why does foreach not bring anything to the driver program?

Why does my program compile but not do anything?

Why can't this program print anything using goroutine?

Why doesn't my program that computes perfect numbers print anything?

Why does my C function doesny print anything?

Why does the program terminate on startup without outputting anything?

why does this particular program print me five?

Why does this program print "forked!" 4 times?

Why does the program not execute print statements in main?

Why does this program print 3 and not 2?

Why this program does not print the desired output?

Why does this program with fork print twice?

why does my program not print the nextLine?

Why does this program print 23 after casting?

My program doesn't print anything for the print("the"). Can anyone explain why?

The lyricsForName function does not print anything

Child process does not print anything

Assembly - Program does not return anything

My program which creates a abbreviation of an char array, does not print anything. Where is my mistake?

SAS: Why does %include not print comments from the included program in the log?

Why does this for loop in C program print Hi 11 times?

Why my following program does not print any output ( ArrayList )?

Why does the program print out an "@" when I enter nothing?

java linked list .This program does not print 8.Why?

Why does a program with fork() sometimes print its output multiple times?