Each()方法对所查找的标签进行遍历循环
doc.Find(".cbox ul li").Each(func(j int, tr *goquery.Selection) { //对clss名字为cbox的标签中的ul中的li 进行循环遍历 title := tr.Find("a").Eq(0).Text() //标题 输出li中第一个a标签的值 detailsLink, ok := tr.Find("a").Eq(0).Attr("href") //详情页地址 //获取li中a的链接值 times := tr.Find("span").Eq(0).Text() // 输出时间 //获取第一个span标签的值 if ok != true { fmt.Println("获取失败") } li有多少个 就会循环次
Find(selection) *Selection //selection填写要查询的标签
Eq(index int) *Selection //填写索引来寻找对应标签集
First() *Selection // 查找第一个标签集
Last() *Selection //获取最后一个标签集
Next() *Selection //获取下一个同标签中的下一个如有两个a 分别是a1 a2 填写了,如果a1写了next 就直接跳转a2
NextAll() *Selection //获取后面所有兄弟节点集 获取a1后边的所有a的值
Prev() *Selection //前一个兄弟节点集 和上边那条相反
Get(index int) *html.Node //根据索引获取一个节点
Index() int //返回选择对象中第一个元素的位置
Slice(start, end int) *Selection //根据起始位置获取子节点集
循环遍历选择的节点
Each(f func(int, *Selection)) *Selection //遍历
EachWithBreak(f func(int, *Selection) bool) *Selection //可中断遍历
Map(f func(int, *Selection) string) (result []string) //返回字符串数组
检测或获取节点属性值
Attr(), RemoveAttr(), SetAttr() //获取,移除,设置属性的值
AddClass(), HasClass(), RemoveClass(), ToggleClass()
Html() //获取该节点的html
Length() //返回该Selection的元素个数
Text() //获取该节点的文本值
————————————————
原文链接:https://blog.csdn.net/yang731227/article/details/89338745,本人来自以上链接,加上本人的个人理解,有大量素材来自链接, 有些还没理解,就先写这么多,做记录