本文整理汇总了Golang中github.com/PuerkitoBio/goquery.Selection.Closest方法的典型用法代码示例。如果您正苦于以下问题:Golang Selection.Closest方法的具体用法?Golang Selection.Closest怎么用?Golang Selection.Closest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/PuerkitoBio/goquery.Selection
的用法示例。
在下文中一共展示了Selection.Closest方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: itemURL
func itemURL(s *goquery.Selection) (url string) {
s.Closest("tr").Next().Find("a").Each(func(_ int, s *goquery.Selection) {
href, _ := s.Attr("href")
if strings.HasPrefix(href, "item?id=") {
url = hnURL + href
}
})
return
}
示例2: guessImgEnvironment
func guessImgEnvironment(s *goquery.Selection) *goquery.Selection {
possibleEnvs := []string{
"figure",
".inline-image", // bbc
".inline-media__unlinked-image", // bloomberg
"header .inline-media", // bloomberg
"bigimgWrap", // bloomberg
".story-img", // technology review
".img-left", // technology review
".article-img", // technology review
".img-wrap", // the intercept
".overlap-img", // nautilus
".inline-content", // washington post
".image", // npr.org
".centered-figure-container", // npr.org
".slate-image", // slate.com
".wp-caption", // mcntyr.com and quantamagazine.com
".article__image", // aeon
}
var possibleParents []*goquery.Selection
for _, env := range possibleEnvs {
parent := s.Closest(env)
if parent.Nodes != nil {
Debug("Found img env: %v", env)
possibleParents = append(possibleParents, parent)
}
}
if len(possibleParents) == 0 {
Debug("No parents found")
return s
}
var htmlPossibleParents []string
for _, p := range possibleParents {
h, _ := p.Html()
htmlPossibleParents = append(htmlPossibleParents, h)
}
indexLongestElement, _ := longestElement(htmlPossibleParents)
Debug("longest Env: %v", htmlPossibleParents[indexLongestElement])
return possibleParents[indexLongestElement]
}
示例3: parentAttr
func parentAttr(s *goquery.Selection, attr string) string {
sel := "[" + attr + "]"
res, _ := s.Closest(sel).Attr(attr)
return res
}