mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
make example easier to understand (used from outside the goquery package)
This commit is contained in:
@@ -56,41 +56,33 @@ Please note that Cascadia's selectors do NOT necessarily match all supported sel
|
||||
|
||||
## Examples
|
||||
|
||||
Taken from example_test.go:
|
||||
Taken (and adapted as if executed from outside the goquery package) from example_test.go:
|
||||
|
||||
```Go
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
package main
|
||||
|
||||
// In real use, this import would be required (not in this example, since it
|
||||
// is part of the goquery package)
|
||||
//"github.com/PuerkitoBio/goquery"
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
// This example scrapes the reviews shown on the home page of metalsucks.net.
|
||||
func ExampleScrape_MetalSucks() {
|
||||
// Load the HTML document (in real use, the type would be *goquery.Document)
|
||||
var doc *Document
|
||||
var e error
|
||||
|
||||
// Initialize doc, (in real use, the method would be goquery.NewDocument)
|
||||
if doc, e = NewDocument("http://metalsucks.net"); e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
func ExampleScrape() {
|
||||
doc, err := goquery.NewDocument("http://metalsucks.net")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Find the review items (the type of the Selection would be *goquery.Selection)
|
||||
doc.Find(".reviews-wrap article .review-rhs").Each(func(i int, s *Selection) {
|
||||
// For each item found, get the band and title
|
||||
band := s.Find("h3").Text()
|
||||
title := s.Find("i").Text()
|
||||
fmt.Printf("Review %d: %s - %s\n", i, band, title)
|
||||
})
|
||||
// To see the output of the Example while running the test suite (go test), simply
|
||||
// remove the leading "x" before Output on the next line. This will cause the
|
||||
// example to fail (all the "real" tests should pass).
|
||||
doc.Find(".reviews-wrap article .review-rhs").Each(func(i int, s *goquery.Selection) {
|
||||
band := s.Find("h3").Text()
|
||||
title := s.Find("i").Text()
|
||||
fmt.Printf("Review %d: %s - %s\n", i, band, title)
|
||||
})
|
||||
}
|
||||
|
||||
// xOutput: voluntarily fail the Example output.
|
||||
func main() {
|
||||
ExampleScrape()
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+3
-5
@@ -12,11 +12,9 @@ import (
|
||||
// This example scrapes the reviews shown on the home page of metalsucks.net.
|
||||
func ExampleScrape_MetalSucks() {
|
||||
// Load the HTML document (in real use, the type would be *goquery.Document)
|
||||
var doc *Document
|
||||
var e error
|
||||
|
||||
if doc, e = NewDocument("http://metalsucks.net"); e != nil {
|
||||
log.Fatal(e)
|
||||
doc, err := NewDocument("http://metalsucks.net")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Find the review items (the type of the Selection would be *goquery.Selection)
|
||||
|
||||
Reference in New Issue
Block a user