本文整理汇总了Golang中github.com/PuerkitoBio/goquery.Selection类的典型用法代码示例。如果您正苦于以下问题:Golang Selection类的具体用法?Golang Selection怎么用?Golang Selection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Selection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getSelectionSignature
// this function returns some specific signature of a selection
// so it can be easy found to get data quickly next time
func getSelectionSignature(s *goquery.Selection) string {
var signature string
tag, _ := goquery.OuterHtml(s)
pos := strings.Index(tag, ">")
if pos > -1 {
tag = tag[1:pos]
} else {
return ""
}
signature = convertTagToJqueryFormat(tag, s)
s.Parents().Each(func(i int, sec *goquery.Selection) {
ohtml, _ := goquery.OuterHtml(sec)
pos := strings.Index(ohtml, ">")
if pos > -1 {
ohtml = ohtml[1:pos]
}
tag := convertTagToJqueryFormat(ohtml, sec)
signature = tag + " " + signature
})
return signature
}
示例2: isHighLinkDensity
//checks the density of links within a node, is there not much text and most of it contains bad links?
//if so it's no good
func (this *contentExtractor) isHighLinkDensity(node *goquery.Selection) bool {
links := node.Find("a")
if links == nil || links.Size() == 0 {
return false
}
text := node.Text()
words := strings.Split(text, " ")
nwords := len(words)
sb := make([]string, 0)
links.Each(func(i int, s *goquery.Selection) {
linkText := s.Text()
sb = append(sb, linkText)
})
linkText := strings.Join(sb, "")
linkWords := strings.Split(linkText, " ")
nlinkWords := len(linkWords)
nlinks := links.Size()
linkDivisor := float64(nlinkWords) / float64(nwords)
score := linkDivisor * float64(nlinks)
if this.config.debug {
logText := ""
if len(node.Text()) >= 51 {
logText = node.Text()[0:50]
} else {
logText = node.Text()
}
log.Printf("Calculated link density score as %1.5f for node %s\n", score, logText)
}
if score > 1.0 {
return true
}
return false
}
示例3: commonPrase
func (b baiduNews) commonPrase(ctx *Context) (infoStr string) {
body := ctx.GetDom().Find("body")
var info *goquery.Selection
if h1s := body.Find("h1"); len(h1s.Nodes) != 0 {
for i := 0; i < len(h1s.Nodes); i++ {
info = b.findP(h1s.Eq(i))
}
} else if h2s := body.Find("h2"); len(h2s.Nodes) != 0 {
for i := 0; i < len(h2s.Nodes); i++ {
info = b.findP(h2s.Eq(i))
}
} else if h3s := body.Find("h3"); len(h3s.Nodes) != 0 {
for i := 0; i < len(h3s.Nodes); i++ {
info = b.findP(h3s.Eq(i))
}
} else {
info = body.Find("body")
}
infoStr, _ = info.Html()
// 清洗HTML
infoStr = CleanHtml(infoStr, 5)
return
}
示例4: parseColors
func parseColors(s *goquery.Selection) string {
colors := ""
s.Each(func(i int, s *goquery.Selection) {
colors += s.Text()
})
return colors
}
示例5: attrOrDefault
// attributeOrDefault reads an attribute and returns it or the default value when it's empty.
func (bow *Browser) attrOrDefault(name, def string, sel *goquery.Selection) string {
a, ok := sel.Attr(name)
if ok {
return a
}
return def
}
示例6: commonPrase
func (b baiduNews) commonPrase(resp *context.Response) (infoStr string) {
body := resp.GetDom().Find("body")
var info *goquery.Selection
if h1s := body.Find("h1"); len(h1s.Nodes) != 0 {
for i := 0; i < len(h1s.Nodes); i++ {
info = b.findP(h1s.Eq(i))
}
} else if h2s := body.Find("h2"); len(h2s.Nodes) != 0 {
for i := 0; i < len(h2s.Nodes); i++ {
info = b.findP(h2s.Eq(i))
}
} else if h3s := body.Find("h3"); len(h3s.Nodes) != 0 {
for i := 0; i < len(h3s.Nodes); i++ {
info = b.findP(h3s.Eq(i))
}
} else {
info = body.Find("body")
}
// 去除标签
// info.RemoveFiltered("script")
// info.RemoveFiltered("style")
infoStr, _ = info.Html()
// 清洗HTML
infoStr = CleanHtml(infoStr, 5)
return
}
示例7: scrapPayload
func scrapPayload(s *goquery.Selection, n int) string {
url, ok := s.Find("a").Attr("href")
if !ok {
die("unable to find URL for scrapping")
}
return scrapPayloadURL("https://developer.github.com"+url, n)
}
示例8: JoinNodesWithSpace
func JoinNodesWithSpace(s *goquery.Selection) string {
texts := []string{}
s.Each(func(i int, s *goquery.Selection) {
texts = append(texts, s.Text())
})
return strings.Join(texts, " ")
}
示例9: dropTag
func (this *parser) dropTag(selection *goquery.Selection) {
selection.Each(func(i int, s *goquery.Selection) {
node := s.Get(0)
node.Data = s.Text()
node.Type = html.TextNode
})
}
示例10: parseGamePosition
func parseGamePosition(selection *goquery.Selection) (position int) {
positionString := strings.TrimSpace(selection.Children().First().Text())
var err error
position, err = strconv.Atoi(strings.TrimSpace(positionString))
helper.HandleFatalError("parsing game position failed:", err)
return
}
示例11: ScrapeExamples
func ScrapeExamples(s *goquery.Selection) []string {
examples := []string{}
s.Find("span.h").Each(func(i int, s *goquery.Selection) {
examples = append(examples, s.Text())
})
return examples
}
示例12: extractCredits
func extractCredits(selection *goquery.Selection) string {
if result := trim(selection.Find(".credits").Text()); strings.Contains(result, "#") {
return "0"
} else {
return result
}
}
示例13: extractCourseDescription
func extractCourseDescription(selection *goquery.Selection) string {
url := trim(fmt.Sprintln(selection.Find(".catalogdescription a").AttrOr("href", "")))
fmt.Println("LOGGING URL", url)
client := http.Client{}
req, _ := http.NewRequest("GET", "http://catalog.njit.edu/ribbit/index.cgi?format=html&page=fsinjector.rjs&fullpage=true", nil)
req.Header.Add("Referer", url)
resp, err := client.Do(req)
if err != nil {
return ""
}
if resp != nil {
defer resp.Body.Close()
}
body, _ := ioutil.ReadAll(resp.Body)
//checkError(err)
result := substringAfter(string(body), "courseblockdesc")
if len(result) < 4 {
return ""
}
result = substringBefore(result[3:], "<b")
if string(result[0]) == "<" || strings.Contains(result, "at SISConnxService") {
return ""
}
result = strings.Replace(result, "\\\"", "\"", -1)
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(result))
return trim(doc.Text())
}
示例14: convertTagToJqueryFormat
func convertTagToJqueryFormat(tag string, s *goquery.Selection) string {
tagitself := tag
pos := strings.Index(tag, " ")
if pos > -1 {
tagitself = tag[0:pos]
} else {
return tag
}
class, found := s.Attr("class")
if found && class != "" {
pos := strings.Index(class, " ")
// leave only a first class from a list
if pos > -1 {
class = class[0:pos]
}
tagitself = tagitself + "." + class
}
return tagitself
}
示例15: testList
func testList(t *testing.T, list *goquery.Selection) {
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
testList(t, items)
items.RemoveFiltered("ul")
})
checkAlphabeticOrder(t, list)
}