package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"strconv"
"strings"
"sync"
"time"
)
func do_task(url string, domain string) {
p, err := goquery.NewDocument(url)
if err != nil {
panic(err)
} else {
p.Find("h3").Find("a").Each(func(i int, selection *goquery.Selection) {
href, _ := selection.Attr("href")
link := domain + href
h, err := goquery.NewDocument(link)
if err != nil {
panic(err)
} else {
title := h.Find(".shici-title").Text()
fmt.Printf("url:%s title:%s \n", link, title)
}
})
}
wg.Done()
}
var wg sync.WaitGroup
func main() {
start := time.Now().UnixNano()
domain := "http://www.shicimingju.com"
urlTemplate := domain + "/chaxun/zuozhe/9_{:num}.html"
pageNum := 50
wg.Add(50)
for page := 1; page <= pageNum; page++ {
url := strings.Replace(urlTemplate, "{:num}", strconv.Itoa(page), -1)
go do_task(url, domain)
}
wg.Wait()
end := time.Now().UnixNano()
fmt.Printf("总耗时:%.3f秒 \n", float32(end - start)/1000000000)
}
go协程
最新推荐文章于 2024-05-03 17:17:01 发布