这里直接贴代码
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"strings"
)
type HotsContent struct {
num int
content string
comment string
url string
}
func main() {
fmt.Println("开始爬取糗事百科热点笑话...")
js, err := goquery.NewDocument("https://www.qiushibaike.com/hot/")
if err != nil {
log.Fatal(err)
}
js.Find("#content-left .article").Each(func(i int, contentSelection *goquery.Selection) {
//先判断是否有图片
img, _ := contentSelection.Find(".thumb a").Attr("href")
if len(img) == 0 {
hotsArt := HotsContent{}
content := contentSelection.Find(".content span").Text()
url, _ := contentSelection.Find(".contentHerf").Attr("href")
comment_name := contentSelection.Find(".cmtMain .cmt-name").Text()
comment_cont := contentSelection.Find(".cmtMain .main-text").Text()
hotsArt.num = i + 1
hotsArt.url = "https://www.qiushibaike.com" + url
hotsArt.content = strings.Replace(content, "\n", "", -1)
hotsArt.comment = strings.Replace(comment_name+comment_cont, "\n", "", -1)
fmt.Println("第", hotsArt.num, "个笑话:")
fmt.Println("\t", hotsArt.content)
fmt.Println("\t 最热评论:" + hotsArt.comment)
fmt.Println("\t 地址", hotsArt.url)
fmt.Println("======================================================")
}
})
}
谢绝吐槽,写着练手玩玩的
1
jjianwen68 2017 年 9 月 5 日
几年前用过 goquery,挺好用
|
2
zbl430 OP @jjianwen68 还可以
|
3
qlbr 2017 年 9 月 5 日
第一次见到活的 go 语言, 原来是酱紫的
|
4
pathletboy 2017 年 9 月 5 日
你已经留了一个坑,结构体字段名首字母最好大写,避免将来踩坑。
|
5
zbl430 OP @pathletboy 有道理,记住了
|
6
yigemeirenyongde 2017 年 9 月 5 日
还在入门 go,前端转 go
|
7
lixuda 2017 年 9 月 5 日
感觉 go 越来越牛
|
8
polaris1119 2017 年 9 月 5 日
go 发展还是挺猛的
|
9
Akkuman 2017 年 9 月 5 日
go 写了几个网站,还行
|