example_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package goquery_test
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "github.com/PuerkitoBio/goquery"
  9. )
  10. // This example scrapes the reviews shown on the home page of metalsucks.net.
  11. func Example() {
  12. // Request the HTML page.
  13. res, err := http.Get("http://metalsucks.net")
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. defer res.Body.Close()
  18. if res.StatusCode != 200 {
  19. log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  20. }
  21. // Load the HTML document
  22. doc, err := goquery.NewDocumentFromReader(res.Body)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. // Find the review items
  27. doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
  28. // For each item found, get the band and title
  29. band := s.Find("a").Text()
  30. title := s.Find("i").Text()
  31. fmt.Printf("Review %d: %s - %s\n", i, band, title)
  32. })
  33. // To see the output of the Example while running the test suite (go test), simply
  34. // remove the leading "x" before Output on the next line. This will cause the
  35. // example to fail (all the "real" tests should pass).
  36. // xOutput: voluntarily fail the Example output.
  37. }
  38. // This example shows how to use NewDocumentFromReader from a file.
  39. func ExampleNewDocumentFromReader_file() {
  40. // create from a file
  41. f, err := os.Open("some/file.html")
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. defer f.Close()
  46. doc, err := goquery.NewDocumentFromReader(f)
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. // use the goquery document...
  51. _ = doc.Find("h1")
  52. }
  53. // This example shows how to use NewDocumentFromReader from a string.
  54. func ExampleNewDocumentFromReader_string() {
  55. // create from a string
  56. data := `
  57. <html>
  58. <head>
  59. <title>My document</title>
  60. </head>
  61. <body>
  62. <h1>Header</h1>
  63. </body>
  64. </html>`
  65. doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. header := doc.Find("h1").Text()
  70. fmt.Println(header)
  71. // Output: Header
  72. }