github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/example_test.go (about) 1 package goquery 2 3 import ( 4 "fmt" 5 // In real use, this import would be required (not in this example, since it 6 // is part of the goquery package) 7 //"github.com/insionng/yougam/libraries/PuerkitoBio/goquery" 8 "strconv" 9 ) 10 11 // This example scrapes the 10 reviews shown on the home page of MetalReview.com, 12 // the best metal review site on the web :) (and no, I'm not affiliated to them!) 13 func ExampleScrape_MetalReview() { 14 // Load the HTML document (in real use, the type would be *goquery.Document) 15 var doc *Document 16 var e error 17 18 if doc, e = NewDocument("http://metalreview.com"); e != nil { 19 panic(e.Error()) 20 } 21 22 // Find the review items (the type of the Selection would be *goquery.Selection) 23 doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) { 24 var band, title string 25 var score float64 26 27 // For each item found, get the band, title and score, and print it 28 band = s.Find("strong").Text() 29 title = s.Find("em").Text() 30 if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil { 31 // Not a valid float, ignore score 32 fmt.Printf("Review %d: %s - %s.\n", i, band, title) 33 } else { 34 // Print all, including score 35 fmt.Printf("Review %d: %s - %s (%2.1f).\n", i, band, title, score) 36 } 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 }