...

Source file src/github.com/PuerkitoBio/goquery/example_test.go

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/PuerkitoBio/goquery"
    11  )
    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() {
    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  }
    83  
    84  func ExampleSingle() {
    85  	html := `
    86  <html>
    87    <body>
    88      <div>1</div>
    89      <div>2</div>
    90      <div>3</div>
    91    </body>
    92  </html>
    93  `
    94  	doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
    95  	if err != nil {
    96  		log.Fatal(err)
    97  	}
    98  
    99  	// By default, the selector string selects all matching nodes
   100  	multiSel := doc.Find("div")
   101  	fmt.Println(multiSel.Text())
   102  
   103  	// Using goquery.Single, only the first match is selected
   104  	singleSel := doc.FindMatcher(goquery.Single("div"))
   105  	fmt.Println(singleSel.Text())
   106  
   107  	// Output:
   108  	// 123
   109  	// 1
   110  }
   111  

View as plain text