File tree 2 files changed +28
-5
lines changed
2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -95,15 +95,27 @@ package main
95
95
import (
96
96
" fmt"
97
97
" log"
98
+ " net/http"
98
99
99
100
" github.com/PuerkitoBio/goquery"
100
101
)
101
102
102
103
func ExampleScrape () {
103
- doc , err := goquery.NewDocument (" http://metalsucks.net" )
104
- if err != nil {
105
- log.Fatal (err)
106
- }
104
+ // Request the HTML page.
105
+ res , err := http.Get (" http://metalsucks.net" )
106
+ if err != nil {
107
+ log.Fatal (err)
108
+ }
109
+ defer res.Body .Close ()
110
+ if res.StatusCode != 200 {
111
+ log.Fatalf (" status code error: %d %s " , res.StatusCode , res.Status )
112
+ }
113
+
114
+ // Load the HTML document
115
+ doc , err := goquery.NewDocumentFromReader (res.Body )
116
+ if err != nil {
117
+ log.Fatal (err)
118
+ }
107
119
108
120
// Find the review items
109
121
doc.Find (" .sidebar-reviews article .content-block" ).Each (func (i int , s *goquery.Selection ) {
Original file line number Diff line number Diff line change @@ -3,14 +3,25 @@ package goquery_test
3
3
import (
4
4
"fmt"
5
5
"log"
6
+ "net/http"
6
7
7
8
"github.com/PuerkitoBio/goquery"
8
9
)
9
10
10
11
// This example scrapes the reviews shown on the home page of metalsucks.net.
11
12
func Example () {
13
+ // Request the HTML page.
14
+ res , err := http .Get ("http://metalsucks.net" )
15
+ if err != nil {
16
+ log .Fatal (err )
17
+ }
18
+ defer res .Body .Close ()
19
+ if res .StatusCode != 200 {
20
+ log .Fatalf ("status code error: %d %s" , res .StatusCode , res .Status )
21
+ }
22
+
12
23
// Load the HTML document
13
- doc , err := goquery .NewDocument ( "http://metalsucks.net" )
24
+ doc , err := goquery .NewDocumentFromReader ( res . Body )
14
25
if err != nil {
15
26
log .Fatal (err )
16
27
}
You can’t perform that action at this time.
0 commit comments