-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_goquery1.go
47 lines (40 loc) · 1 KB
/
test_goquery1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func ExampleScrape() {
// Request the HTML page.
res, err := http.Get("http://metalsucks.net")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the review items
//doc.Find(".left-content article .post-title").Each(func(i int, s *goquery.Selection) {
/*
doc.Find().Each(func(i int, s *goquery.Selection) {
// For each item found, get the title
title := s.Find("a").Text()
fmt.Printf("Review %d: %s\n", i, title)
})
*/
doc.Find("a").Each(func(i int, s *goquery.Selection) {
// For each item found, get the title
title := s.Text()
fmt.Printf("Link %d: %s\n", i, title)
})
}
func main() {
ExampleScrape()
}