当前位置: 首页>>代码示例>>Golang>>正文


Golang cascadia.MustCompile函数代码示例

本文整理汇总了Golang中code/google/com/p/cascadia.MustCompile函数的典型用法代码示例。如果您正苦于以下问题:Golang MustCompile函数的具体用法?Golang MustCompile怎么用?Golang MustCompile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了MustCompile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetFeedUrl

func GetFeedUrl(u string) (string, error) {
	resp, err := http.Get(u)
	if err != nil {
		return "", err
	}

	if strings.Contains(resp.Header.Get("Content-Type"), "xml") {
		return u, nil
	}

	tree, err := html.Parse(resp.Body)
	if err != nil {
		return "", err
	}

	sel := cascadia.MustCompile("link[rel=alternate][type*=xml]")
	alt := sel.MatchFirst(tree)
	if alt == nil {
		return "", errors.New("no feed link found")
	}

	altUrl, found := FindAttr("href", alt.Attr)
	if !found {
		return "", errors.New("missing link in alternate")
	}

	return ToAbsolute(resp.Request.URL, altUrl.Val), nil
}
开发者ID:heyLu,项目名称:lp,代码行数:28,代码来源:feeds.go

示例2: Is

// Is() checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (this *Selection) Is(selector string) bool {
	if len(this.Nodes) > 0 {
		// The selector must be done on the document if it has positional criteria

		// TODO : Not sure it is required, as Cascadia's selector checks within the parent of the
		// node when there is such a positionaly selector... In jQuery, this is for the
		// non-css selectors (Sizzle-implemented selectors, an extension of CSS)

		/*if ok, e := regexp.MatchString(rxNeedsContext, selector); ok {
			sel := this.document.Root.Find(selector)
			for _, n := range this.Nodes {
				if sel.IndexOfNode(n) > -1 {
					return true
				}
			}

		} else if e != nil {
			panic(e.Error())

		} else {*/
		// Attempt a match with the selector
		cs := cascadia.MustCompile(selector)
		if len(this.Nodes) == 1 {
			return cs.Match(this.Nodes[0])
		} else {
			return len(cs.Filter(this.Nodes)) > 0
		}
		//}
	}

	return false
}
开发者ID:ikbear,项目名称:goquery,代码行数:34,代码来源:query.go

示例3: Is

// Is checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (s *Selection) Is(selector string) bool {
	if len(s.Nodes) > 0 {
		return s.IsMatcher(cascadia.MustCompile(selector))
	}

	return false
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:9,代码来源:query.go

示例4: GetPageInfo

func GetPageInfo(u string) (*PageInfo, error) {
	res, err := http.Get(u)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	tree, err := html.Parse(res.Body)
	if err != nil {
		return nil, err
	}

	sel := cascadia.MustCompile("meta")
	meta := sel.MatchAll(tree)

	found, title := findTitle(tree)
	if !found {
		_, title = findProperty(meta, "title", "twitter:title")
	}

	_, description := findProperty(meta, "description", "og:description")
	_, image := findProperty(meta, "og:image")

	return &PageInfo{
		Title:       title,
		Description: description,
		Image:       image,
		RawURL:      u,
	}, nil
}
开发者ID:heyLu,项目名称:lp,代码行数:30,代码来源:inquire.go

示例5: Is

// Is checks the current matched set of elements against a selector and
// returns true if at least one of these elements matches.
func (s *Selection) Is(selector string) bool {
	if len(s.Nodes) > 0 {
		// Attempt a match with the selector
		cs := cascadia.MustCompile(selector)
		if len(s.Nodes) == 1 {
			return cs.Match(s.Nodes[0])
		}
		return len(cs.Filter(s.Nodes)) > 0
	}

	return false
}
开发者ID:hu17889,项目名称:goquery,代码行数:14,代码来源:query.go

示例6: winnow

// Filter based on a selector string, and the indicator to keep (Filter) or
// to get rid of (Not) the matching elements.
func winnow(sel *Selection, selector string, keep bool) []*html.Node {
	cs := cascadia.MustCompile(selector)

	// Optimize if keep is requested
	if keep {
		return cs.Filter(sel.Nodes)
	}
	// Use grep
	return grep(sel, func(i int, s *Selection) bool {
		return !cs.Match(s.Get(0))
	})
}
开发者ID:hu17889,项目名称:goquery,代码行数:14,代码来源:filter.go

示例7: Closest

// Closest() gets the first element that matches the selector by testing the
// element itself and traversing up through its ancestors in the DOM tree.
func (this *Selection) Closest(selector string) *Selection {
	cs := cascadia.MustCompile(selector)

	return pushStack(this, mapNodes(this.Nodes, func(i int, n *html.Node) []*html.Node {
		// For each node in the selection, test the node itself, then each parent
		// until a match is found.
		for ; n != nil; n = n.Parent {
			if cs.Match(n) {
				return []*html.Node{n}
			}
		}
		return nil
	}))
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:16,代码来源:traversal.go

示例8: findWithSelector

// Internal implementation of Find that return raw nodes.
func findWithSelector(nodes []*html.Node, selector string) []*html.Node {
	// Compile the selector once
	sel := cascadia.MustCompile(selector)
	// Map nodes to find the matches within the children of each node
	return mapNodes(nodes, func(i int, n *html.Node) (result []*html.Node) {
		// Go down one level, becausejQuery's Find() selects only within descendants
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			if c.Type == html.ElementNode {
				result = append(result, sel.MatchAll(c)...)
			}
		}
		return
	})
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:15,代码来源:traversal.go

示例9: findTitle

func findTitle(tree *html.Node) (found bool, title string) {
	sel := cascadia.MustCompile("title")
	node := sel.MatchFirst(tree)
	if node == nil {
		return false, ""
	}

	if node.Type == html.ElementNode {
		node = node.FirstChild
	}

	buf := new(bytes.Buffer)
	for node != nil {
		if node.Type == html.TextNode {
			buf.WriteString(node.Data)
		}

		node = node.NextSibling
	}

	return true, string(buf.Bytes())
}
开发者ID:heyLu,项目名称:lp,代码行数:22,代码来源:inquire.go

示例10: GetFavicon

func GetFavicon(url string) (string, error) {
	if favicon, err := GetCanonicalFavicon(url); err == nil {
		fmt.Println("found favicon.ico")
		return favicon, nil
	} else if *debug {
		fmt.Printf("Error: getting /favicon.ico: %s\n", err)
	}

	resp, err := http.Get(url)
	if *debug {
		fmt.Println("get html", resp, err)
	}
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	tree, err := html.Parse(resp.Body)
	if *debug {
		fmt.Println("parse html", tree, err)
	}
	if err != nil {
		return "", err
	}

	sel := cascadia.MustCompile("link[rel~=icon]")
	node := sel.MatchFirst(tree)
	if node == nil {
		return "", errors.New("no favicon found")
	}

	favicon, found := FindAttr("href", node.Attr)
	if !found {
		return "", errors.New("no link found")
	}

	return ToAbsolute(resp.Request.URL, favicon.Val), nil
}
开发者ID:heyLu,项目名称:lp,代码行数:38,代码来源:favicon.go

示例11: Closest

// Closest gets the first element that matches the selector by testing the
// element itself and traversing up through its ancestors in the DOM tree.
func (s *Selection) Closest(selector string) *Selection {
	cs := cascadia.MustCompile(selector)
	return s.ClosestMatcher(cs)
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:6,代码来源:traversal.go

示例12: WrapInner

// WrapInner wraps an HTML structure, matched by the given selector, around the
// content of element in the set of matched elements. The matched child is
// cloned before being inserted into the document.
//
// It returns the original set of elements.
func (s *Selection) WrapInner(selector string) *Selection {
	return s.WrapInnerMatcher(cascadia.MustCompile(selector))
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:8,代码来源:manipulation.go

示例13: Append

// Append appends the elements specified by the selector to the end of each element
// in the set of matched elements, following those rules:
//
// 1) The selector is applied to the root document.
//
// 2) Elements that are part of the document will be moved to the new location.
//
// 3) If there are multiple locations to append to, cloned nodes will be
// appended to all target locations except the last one, which will be moved
// as noted in (2).
func (s *Selection) Append(selector string) *Selection {
	return s.AppendMatcher(cascadia.MustCompile(selector))
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:13,代码来源:manipulation.go

示例14: RemoveFiltered

// RemoveFiltered removes the set of matched elements by selector.
// It returns the Selection of removed nodes.
func (s *Selection) RemoveFiltered(selector string) *Selection {
	return s.RemoveMatcher(cascadia.MustCompile(selector))
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:5,代码来源:manipulation.go

示例15: ReplaceWith

// ReplaceWith replaces each element in the set of matched elements with the
// nodes matched by the given selector.
// It returns the removed elements.
//
// This follows the same rules as Selection.Append.
func (s *Selection) ReplaceWith(selector string) *Selection {
	return s.ReplaceWithMatcher(cascadia.MustCompile(selector))
}
开发者ID:lucmichalski,项目名称:crawler,代码行数:8,代码来源:manipulation.go


注:本文中的code/google/com/p/cascadia.MustCompile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。