This example scrapes the reviews shown on the home page of metalsucks.net.
()
| 12 | |
| 13 | // This example scrapes the reviews shown on the home page of metalsucks.net. |
| 14 | func Example() { |
| 15 | // Request the HTML page. |
| 16 | res, err := http.Get("http://metalsucks.net") |
| 17 | if err != nil { |
| 18 | log.Fatal(err) |
| 19 | } |
| 20 | defer res.Body.Close() |
| 21 | if res.StatusCode != 200 { |
| 22 | log.Fatalf("status code error: %d %s", res.StatusCode, res.Status) |
| 23 | } |
| 24 | |
| 25 | // Load the HTML document |
| 26 | doc, err := goquery.NewDocumentFromReader(res.Body) |
| 27 | if err != nil { |
| 28 | log.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | // Find the review items |
| 32 | doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) { |
| 33 | // For each item found, get the band and title |
| 34 | band := s.Find("a").Text() |
| 35 | title := s.Find("i").Text() |
| 36 | fmt.Printf("Review %d: %s - %s\n", i, band, title) |
| 37 | }) |
| 38 | // To see the output of the Example while running the test suite (go test), simply |
| 39 | // remove the leading "x" before Output on the next line. This will cause the |
| 40 | // example to fail (all the "real" tests should pass). |
| 41 | |
| 42 | // xOutput: voluntarily fail the Example output. |
| 43 | } |
| 44 | |
| 45 | // This example shows how to use NewDocumentFromReader from a file. |
| 46 | func ExampleNewDocumentFromReader_file() { |
nothing calls this directly
no test coverage detected
searching dependent graphs…