File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 4
4
"fmt"
5
5
"log"
6
6
"net/http"
7
+ "os"
8
+ "strings"
7
9
8
10
"github.com/PuerkitoBio/goquery"
9
11
)
@@ -39,3 +41,42 @@ func Example() {
39
41
40
42
// xOutput: voluntarily fail the Example output.
41
43
}
44
+
45
+ // This example shows how to use NewDocumentFromReader from a file.
46
+ func ExampleNewDocumentFromReader_file () {
47
+ // create from a file
48
+ f , err := os .Open ("some/file.html" )
49
+ if err != nil {
50
+ log .Fatal (err )
51
+ }
52
+ defer f .Close ()
53
+ doc , err := goquery .NewDocumentFromReader (f )
54
+ if err != nil {
55
+ log .Fatal (err )
56
+ }
57
+ // use the goquery document...
58
+ _ = doc .Find ("h1" )
59
+ }
60
+
61
+ // This example shows how to use NewDocumentFromReader from a string.
62
+ func ExampleNewDocumentFromReader_string () {
63
+ // create from a string
64
+ data := `
65
+ <html>
66
+ <head>
67
+ <title>My document</title>
68
+ </head>
69
+ <body>
70
+ <h1>Header</h1>
71
+ </body>
72
+ </html>`
73
+
74
+ doc , err := goquery .NewDocumentFromReader (strings .NewReader (data ))
75
+ if err != nil {
76
+ log .Fatal (err )
77
+ }
78
+ header := doc .Find ("h1" ).Text ()
79
+ fmt .Println (header )
80
+
81
+ // Output: Header
82
+ }
You can’t perform that action at this time.
0 commit comments