看源码,弄清楚各个结构体

https://github.com/PuerkitoBio/goquery#api


package main

import (
"fmt"
"log"
"net/http"

"github.com/PuerkitoBio/goquery"
)
/*
<ul class="app-news-detail__meta"><li class="app-news-detail__meta-item">2018-08-30 11:54:44</li><!----></ul>
document.getElementsByClassName("app-news-detail__meta-item").length = 1

*/
func ExampleScrape() {
// Request the HTML page.
res, err := http.Get("http://cn.sonhoo.com/wukong/a182281")
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(".app-news-detail__meta-item").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
band := s.Find("a").Text()
title := s.Find("i").Text()
fmt.Printf("Review %d: %s - %s ", i, band, title)
// 目标值
ii := s.Text()
fmt.Println(ii)
})
// https://github.com/PuerkitoBio/goquery#api
// 看源码,弄清楚各个结构体
i := doc.Find(".app-news-detail__meta-item")
for _, v := range i.Nodes {
fmt.Println(v.FirstChild.Data)
break
}
}

func main() {
ExampleScrape()
}



原文地址:https://www.cnblogs.com/rsapaper/p/9566416.html