mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
edit example to follow Go conventions
This commit is contained in:
@@ -67,7 +67,7 @@ Please note that Cascadia's selectors do not necessarily match all supported sel
|
||||
|
||||
See some tips and tricks in the [wiki][].
|
||||
|
||||
Taken (and adapted as if executed from outside the goquery package) from example_test.go:
|
||||
Adapted from example_test.go:
|
||||
|
||||
```Go
|
||||
package main
|
||||
|
||||
+7
-9
@@ -1,24 +1,22 @@
|
||||
package goquery
|
||||
package goquery_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
// In real use, this import would be required (not in this example, since it
|
||||
// is part of the goquery package)
|
||||
//"github.com/PuerkitoBio/goquery"
|
||||
"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)
|
||||
doc, err := NewDocument("http://metalsucks.net")
|
||||
func Example() {
|
||||
// Load the HTML document
|
||||
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) {
|
||||
// Find the review items
|
||||
doc.Find(".reviews-wrap article .review-rhs").Each(func(i int, s *goquery.Selection) {
|
||||
// For each item found, get the band and title
|
||||
band := s.Find("h3").Text()
|
||||
title := s.Find("i").Text()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
@@ -190,3 +191,15 @@ func TestNewDocumentFromResponseNil(t *testing.T) {
|
||||
t.Error("Expected error, got none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssue103(t *testing.T) {
|
||||
d, err := NewDocumentFromReader(strings.NewReader("<html><title>Scientists Stored These Images in DNA—Then Flawlessly Retrieved Them</title></html>"))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
text := d.Find("title").Text()
|
||||
for i, r := range text {
|
||||
fmt.Printf("%d: %d - %q\n", i, r, string(r))
|
||||
}
|
||||
fmt.Println(text)
|
||||
}
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ func TestNodeNameMultiSel(t *testing.T) {
|
||||
sort.Strings(in)
|
||||
sort.Strings(out)
|
||||
if !reflect.DeepEqual(in, out) {
|
||||
t.Error("want %v, got %v")
|
||||
t.Errorf("want %v, got %v", in, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user