當前位置: 首頁>>代碼示例>>Golang>>正文


Golang selector.Selector函數代碼示例

本文整理匯總了Golang中code/google/com/p/go-html-transform/css/selector.Selector函數的典型用法代碼示例。如果您正苦於以下問題:Golang Selector函數的具體用法?Golang Selector怎麽用?Golang Selector使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Selector函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ParseEntry

func ParseEntry(r io.Reader) (*AmebloEntry, error) {
	root, err := html.Parse(r)
	if err != nil {
		return nil, err
	}
	s, _ := selector.Selector(".articleText")
	nodes := s.Find(root)
	if len(nodes) == 0 {
		return nil, nil
	}
	content := h5.RenderNodesToString(nodes)

	s, _ = selector.Selector("title")
	nodes = s.Find(root)
	if len(nodes) == 0 {
		return nil, nil
	}
	title := extractText(nodes[0].FirstChild)

	entry := &AmebloEntry{
		Title:   strings.Split(title, "|")[0],
		Content: content,
	}
	return entry, nil
}
開發者ID:speedland,項目名稱:lib,代碼行數:25,代碼來源:ameblo.go

示例2: parseClosedSearchResult

func parseClosedSearchResult(r io.Reader) ([]string, bool, error) {
	hasMore := false
	root, err := html.Parse(r)
	if err != nil {
		return nil, false, err
	}
	s, _ := css.Selector("td.bi a")
	nodes := s.Find(root)
	if len(nodes) > 0 {
		hasMore = len(nodes) == CLOSED_SEARCH_PARAM_N
		ids := make([]string, 0)
		for _, n := range nodes {
			for _, attr := range n.Attr {
				if attr.Key == "href" {
					if _url, err := url.Parse(attr.Val); err == nil {
						id := _url.Query().Get("aID")
						if id != "" {
							ids = append(ids, id)
						}
					}
				}
			}
		}
		return ids, hasMore, nil
	} else {
		return nil, false, nil
	}
}
開發者ID:speedland,項目名稱:apps,代碼行數:28,代碼來源:crawler.go

示例3: findOne

func findOne(sel string, node *html.Node) *html.Node {
	s, _ := selector.Selector(sel)
	n := s.Find(node)
	if n == nil || len(n) == 0 {
		return nil
	}
	return n[0]
}
開發者ID:speedland,項目名稱:lib,代碼行數:8,代碼來源:ameblo.go

示例4: getTagValueAsString

func getTagValueAsString(node *html.Node, selector string) string {
	s, _ := css.Selector(selector)
	nodes := s.Find(node)
	if len(nodes) > 0 {
		if children := h5.Children(nodes[0]); len(children) > 0 {
			return children[0].Data
		}
	}
	return ""
}
開發者ID:speedland,項目名稱:apps,代碼行數:10,代碼來源:crawler.go

示例5: parseSearchApiResult

func parseSearchApiResult(r io.Reader) ([]*AuctionItem, bool, error) {
	hasMore := false
	root, err := html.Parse(r)
	if err != nil {
		return nil, false, err
	}
	s, _ := css.Selector("ResultSet")
	nodes := s.Find(root)
	if len(nodes) > 0 {
		var total, returned, firstPos int64
		for _, attr := range nodes[0].Attr {
			switch attr.Key {
			case "totalresultsavailable":
				total, _ = strconv.ParseInt(attr.Val, 0, 0)
				break
			case "totalresultsreturned":
				returned, _ = strconv.ParseInt(attr.Val, 0, 0)
				break
			case "firstresultposition":
				firstPos, _ = strconv.ParseInt(attr.Val, 0, 0)
				break
			default:
				break
			}
		}
		hasMore = firstPos+returned-1 < total
	} else {
		return nil, false, fmt.Errorf("Invalid response and failed to parse pagination data.")
	}

	s, _ = css.Selector("ResultSet Result Item")
	nodes = s.Find(root)
	if len(nodes) > 0 {
		list := make([]*AuctionItem, len(nodes))
		for i, n := range nodes {
			list[i] = parseItemNode(n)
		}
		return list, hasMore, nil
	}
	return nil, false, nil
}
開發者ID:speedland,項目名稱:apps,代碼行數:41,代碼來源:crawler.go

示例6: ApplyToFirstMatch

func (t *Transformer) ApplyToFirstMatch(f TransformFunc, sels ...string) error {
	cs := make([]Collector, 0, len(sels))
	for _, sel := range sels {
		sq, err := selector.Selector(sel)
		if err != nil {
			return err
		}
		cs = append(cs, sq)
	}
	t.ApplyWithCollector(f, FirstMatch(cs...))
	return nil
}
開發者ID:jShi-git,項目名稱:goquery_sample,代碼行數:12,代碼來源:transform.go

示例7: extractMatchingHtmlNodes

func extractMatchingHtmlNodes(response *http.Response, cssSelector string) []*html.Node {
	tree, err := h5.New(response.Body)
	if err != nil {
		log.Fatalf("Error parsing body into tree: %v\n", err)
	}

	selectorChain, err := selector.Selector(cssSelector)
	if err != nil {
		log.Fatalf("Error parsing cssSelector %v: %v\n", cssSelector, err)
	}

	return selectorChain.Find(tree.Top())
}
開發者ID:joho,項目名稱:das_downloader,代碼行數:13,代碼來源:main.go

示例8: getTitleNode

func getTitleNode(document *h5.Tree) (titleNode string, err error) {
	var chain *selector.Chain

	if chain, err = selector.Selector("title"); err != nil {
		return
	}

	if matches := chain.Find(document.Top()); len(matches) > 0 {
		match := matches[0:1]
		titleNode = h5.RenderNodesToString(match)
	}

	return
}
開發者ID:happypancake,項目名稱:go-pjax,代碼行數:14,代碼來源:PjaxFilter.go

示例9: getCanonicalUrl

func getCanonicalUrl(root *html.Node) string {
	s, _ := selector.Selector("head link")
	nodes := s.Find(root)
	for _, n := range nodes {
		isCanonical := false
		href := ""
		for _, attr := range n.Attr {
			if attr.Key == "rel" {
				isCanonical = attr.Val == "canonical"
			}
			if attr.Key == "href" {
				href = attr.Val
			}
		}
		if isCanonical {
			return href
		}
	}
	return ""
}
開發者ID:speedland,項目名稱:apps,代碼行數:20,代碼來源:importer.go

示例10: ParseRss

// Parse the RSS feed and returns iEPG Ids.
func ParseRss(r io.Reader) ([]string, error) {
	root, err := html.Parse(r)
	if err != nil {
		return nil, fmt.Errorf("Could not parse RSS feed: %v", err)
	}
	s, _ := selector.Selector("item")
	nodes := s.Find(root)
	list := []string{}
	for _, n := range nodes {
		for i := range n.Attr {
			if n.Attr[i].Key == "rdf:about" {
				id := extractIdFromUrl(n.Attr[i].Val)
				if id != "" {
					list = append(list, id)
				}
			}
		}
	}
	return list, nil
}
開發者ID:speedland,項目名稱:lib,代碼行數:21,代碼來源:iepg.go

示例11: getGroupKeyword

func getGroupKeyword(n *html.Node) string {
	s, _ := selector.Selector("#news_detail .icon_box .icon-name")
	nodes := s.Find(n)
	if len(nodes) > 0 {
		s := strings.TrimSpace(
			html.UnescapeString(
				extractNodeString(nodes[0]),
			),
		)
		// we don't need the year suffix.
		if strings.HasPrefix(s, "モーニング娘。") {
			return "モーニング娘。"
		}
		// TODO: any other imports than ハロコン?
		if s == "HELLO! PROJECT" {
			return "ハロコン"
		}
		return s
	}
	return ""
}
開發者ID:speedland,項目名稱:apps,代碼行數:21,代碼來源:helloproject.go

示例12: ParseEntryList

func ParseEntryList(r io.Reader) ([]*AmebloEntry, error) {
	root, err := html.Parse(r)
	if err != nil {
		return nil, err
	}
	s, _ := selector.Selector("ul.contentsList li")
	nodes := s.Find(root)
	entryList := make([]*AmebloEntry, 0)

	for _, listItem := range nodes {
		e := &AmebloEntry{}
		// title & url
		n := findOne("a.contentTitle", listItem)
		e.Title = extractText(n.FirstChild)
		e.Url = getAttributeValue("href", n)
		// postAt
		n = findOne(".contentTime time", listItem)
		e.PostAt, err = time.ParseInLocation(TIME_FORMAT, extractText(n.FirstChild), JST)
		if err != nil {
			continue
		}
		div := findOne(".contentDetailArea", listItem)
		// AmLikes and AmComments
		n = findOne("a.contentComment", div)
		if n != nil {
			e.AmComments, _ = strconv.Atoi(
				numRegexp.FindString(extractText(n.FirstChild)),
			)
		}
		n = findOne("a.skinWeakColor", div)
		if n != nil {
			e.AmLikes, _ = strconv.Atoi(
				numRegexp.FindString(extractText(n.FirstChild)),
			)
		}
		entryList = append(entryList, e)
	}
	return entryList, nil
}
開發者ID:speedland,項目名稱:lib,代碼行數:39,代碼來源:ameblo.go

示例13: rewriteBody

func rewriteBody(containerSelector string, dest io.Writer, body string) (err error) {
	if containerSelector == "" {
		dest.Write([]byte(body))
		return
	}

	var chain *selector.Chain
	var document *h5.Tree

	if document, err = h5.NewFromString(body); err != nil {
		err = fmt.Errorf("invalid html document: %v", err)
		return
	}

	var titleNode string
	if titleNode, err = getTitleNode(document); err != nil {
		return
	}

	if chain, err = selector.Selector(containerSelector); err != nil {
		err = fmt.Errorf("invalid css: %v", containerSelector)
		return
	}

	if matches := chain.Find(document.Top()); len(matches) > 0 {
		match := matches[0:1] // Take only the first match
		newBody := h5.RenderNodesToString(h5.Children(match[0]))

		fmt.Printf("data: %v", h5.Data(match[0]))

		dest.Write([]byte(titleNode))
		dest.Write([]byte(newBody))
		return
	}

	err = fmt.Errorf("container not found")
	return
}
開發者ID:happypancake,項目名稱:go-pjax,代碼行數:38,代碼來源:PjaxFilter.go

示例14: Build

// Build will construct a Java Docset for Dash, using the Javadoc contained in
// the javadocPath provided.
func Build(javadocPath string, docsetRoot, docsetName string) error {
	if exists, err := pathExists(javadocPath); !exists {
		return errors.New("javadoc path does not exist")
	} else if err != nil {
		return err
	}

	if exists, err := pathExists(docsetRoot); !exists {
		return errors.New("docset root path does not exist")
	} else if err != nil {
		return err
	}

	docsetPath := filepath.Join(docsetRoot, docsetName+".docset")
	contentsPath := filepath.Join(docsetPath, "Contents")
	resourcesDir := filepath.Join(contentsPath, "Resources")
	documentsDir := filepath.Join(resourcesDir, "Documents")
	if err := os.MkdirAll(documentsDir, 0755); err != nil {
		return err
	}

	if err := copyPath(javadocPath, documentsDir); err != nil {
		return err
	}

	plistPath := filepath.Join(contentsPath, "Info.plist")
	if err := writePlist(plistPath, docsetName); err != nil {
		return err
	}

	indexFile, err := os.Open(filepath.Join(javadocPath, "index-all.html"))
	if err != nil {
		return err
	}

	defer indexFile.Close()

	db, err := initDb(filepath.Join(resourcesDir, "docSet.dsidx"))
	if err != nil {
		return err
	}

	defer db.Close()

	tree, err := h5.New(indexFile)
	if err != nil {
		return err
	}

	itemSelector, err := selector.Selector("dl dt")
	if err != nil {
		return err
	}

	anchorSelector, err := selector.Selector("a")
	if err != nil {
		return err
	}

	for _, node := range itemSelector.Find(tree.Top()) {
		text := nodeText(node, false)
		anchor := anchorSelector.Find(node)[0]
		itemType := ""

		switch {
		case strings.Contains(text, "Class in"):
			itemType = "Class"
		case strings.Contains(text, "Static method in"):
			itemType = "Method"
		case strings.Contains(text, "Static variable in"):
			itemType = "Field"
		case strings.Contains(text, "Constructor"):
			itemType = "Constructor"
		case strings.Contains(text, "Method in"):
			itemType = "Method"
		case strings.Contains(text, "Variable in"):
			itemType = "Field"
		case strings.Contains(text, "Interface in"):
			itemType = "Interface"
		case strings.Contains(text, "Exception in"):
			itemType = "Exception"
		case strings.Contains(text, "Error in"):
			itemType = "Error"
		case strings.Contains(text, "Enum in"):
			itemType = "Enum"
		case strings.Contains(text, "package"):
			itemType = "Package"
		case strings.Contains(text, "Annotation Type"):
			itemType = "Notation"
		}

		tx, err := db.Begin()
		if err != nil {
			return err
		}

		statement, err := tx.Prepare("insert into searchIndex(name, type, path) VALUES(?, ?, ?)")
		if err != nil {
//.........這裏部分代碼省略.........
開發者ID:tatarhy,項目名稱:go-dash-javadocset,代碼行數:101,代碼來源:javadocset.go

示例15: Trans

// Trans creates a Transform that you can apply using ApplyAll.
// It takes a TransformFunc and a valid CSS3 Selector.
// It returns a *Transform or an error if the selector wasn't valid
func Trans(f TransformFunc, sel string) (*Transform, error) {
	sq, err := selector.Selector(sel)
	return TransCollector(f, sq), err
}
開發者ID:jShi-git,項目名稱:goquery_sample,代碼行數:7,代碼來源:transform.go


注:本文中的code/google/com/p/go-html-transform/css/selector.Selector函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。