MCPcopy Index your code
hub / github.com/PuerkitoBio/goquery / Example

Function Example

example_test.go:14–43  ·  view source on GitHub ↗

This example scrapes the reviews shown on the home page of metalsucks.net.

()

Source from the content-addressed store, hash-verified

12
13// This example scrapes the reviews shown on the home page of metalsucks.net.
14func 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.
46func ExampleNewDocumentFromReader_file() {

Callers

nothing calls this directly

Calls 5

NewDocumentFromReaderFunction · 0.92
GetMethod · 0.80
EachMethod · 0.80
FindMethod · 0.80
TextMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…