1

I'm writing a parser HTML in Go. I need to get HTML and pass it to another function.

I did it so:

  1. Can`t pass "doc" to another function
receivedURL, err := http.Get("http://lavillitacafe.com/")
doc, err := goquery.NewDocumentFromReader(receivedURL.Body)
//"linkScrape" this is another function
contactURL := linkScrape(doc)

and

  1. HTML is transferred in parts to another function.
resp, err := http.Get("http://lavillitacafe.com/")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    for true {

        bs := make([]byte, 1014)
        n, err := resp.Body.Read(bs)
                contactURL := linkScrape(bs[:n])
        if n == 0 || err != nil{
            break
        }
    }

How do I do it right?

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412

1 Answers1

0

Here's the basic goquery example adjusted to your use case:

package main

import (
    "fmt"
    "log"
    "strings"

    "github.com/PuerkitoBio/goquery"
)

func findHeader(d *goquery.Document) string {
    header := d.Find("h1").Text()
    return header
}

func main() {
    // create from a string
    data := `
<html>
    <head>
        <title>My document</title>
    </head>
    <body>
        <h1>Header</h1>
    </body>
</html>`

    doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(findHeader(doc))
}
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412