package main
import (
"fmt"
"github.com/gocolly/colly"
"log"
"math/rand"
)
func main() {
fmt.Println(GetHanZiPhotoByColly())
}
func GetHanZiPhotoByColly() (string, error) {
var i int
var err error
j := rand.Intn(25)
img_url := "https://www.aiimooc.com/file/upload/201803/09/140501241.jpg"
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"),
)
c.OnRequest(func(r *colly.Request) {
log.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, erring error) {
err = erring
log.Println("Erroring:", erring)
})
c.OnResponse(func(r *colly.Response) {
log.Println("Visited", r.Request.URL)
})
c.OnHTML("#content .item-img-box img", func(e *colly.HTMLElement) {
if i == j {
img_url = e.Attr("src")
}
i++
})
c.OnScraped(func(r *colly.Response) {
log.Println("Finished", r.Request.URL, i, j)
})
c.Visit(fmt.Sprintf("http://www.shuaia.net/e/tags/index.php?page=%v&tagname=%v&line=25&tempid=3",rand.Intn(16),"帅哥"))
if err != nil {
return img_url, err
} else {
return img_url, nil
}
}