mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-13 12:33:51 +08:00
add examples for NewDocumentFromReader (#254)
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
@@ -39,3 +41,42 @@ func Example() {
|
||||
|
||||
// xOutput: voluntarily fail the Example output.
|
||||
}
|
||||
|
||||
// This example shows how to use NewDocumentFromReader from a file.
|
||||
func ExampleNewDocumentFromReader_file() {
|
||||
// create from a file
|
||||
f, err := os.Open("some/file.html")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
doc, err := goquery.NewDocumentFromReader(f)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// use the goquery document...
|
||||
_ = doc.Find("h1")
|
||||
}
|
||||
|
||||
// This example shows how to use NewDocumentFromReader from a string.
|
||||
func ExampleNewDocumentFromReader_string() {
|
||||
// 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)
|
||||
}
|
||||
header := doc.Find("h1").Text()
|
||||
fmt.Println(header)
|
||||
|
||||
// Output: Header
|
||||
}
|
||||
|
Reference in New Issue
Block a user