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


Golang html.Parse函數代碼示例

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


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

示例1: FetchFullDescription

func FetchFullDescription(link string) string {
	res, err := http.Get(link)
	if err != nil {
		log.Fatal(err)
	}
	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	doc, err := html.Parse(strings.NewReader(string(body)))
	content := ""
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "section" {
			for _, a := range n.Attr {
				if a.Key == "class" && a.Val == "entry-content cf" {
					var buf bytes.Buffer
					html.Render(&buf, n)
					content = buf.String()
					break
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)
	return content
}
開發者ID:john-griffin,項目名稱:bcool,代碼行數:31,代碼來源:bcool.go

示例2: GetEntries

func GetEntries(root string, useSummary bool) (entries []*Entry, err error) {
	filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if strings.ToLower(filepath.Ext(path)) != ".txt" {
			return nil
		}
		entry, _ := GetEntry(path)
		if entry == nil {
			return nil
		}
		entries = append(entries, entry)
		if useSummary {
			doc, err := html.Parse(strings.NewReader(entry.Body))
			if err == nil {
				if text, err := toText(doc); err == nil {
					if len(text) > 500 {
						text = text[0:500] + "..."
					}
					entry.Body = text
				}
			}
		}
		entry.Id = entry.Filename[len(root):len(entry.Filename)-3] + "html"
		return nil
	})
	return
}
開發者ID:fjj596894378,項目名稱:blogo,代碼行數:26,代碼來源:blogo.go

示例3: 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

示例4: fixHtml

// FixHtml parses bytes as HTML and returns well-formed HTML if the parse
// was successful, or escaped HTML, if not.
func fixHtml(linkUrl string, wild []byte) (well []byte) {
	n, err := html.Parse(bytes.NewReader(wild))
	if err != nil {
		return []byte(html.EscapeString(string(wild)))
	}

	fixImgs(linkUrl, n)

	defer func() {
		if err := recover(); err == bytes.ErrTooLarge {
			well = []byte(html.EscapeString(string(wild)))
		} else if err != nil {
			panic(err)
		}
	}()
	buf := bytes.NewBuffer(make([]byte, 0, len(wild)*2))
	if err := html.Render(buf, n); err != nil {
		return []byte(html.EscapeString(string(wild)))
	}

	well = buf.Bytes()
	openBody := []byte("<body>")
	i := bytes.Index(well, openBody)
	if i < 0 {
		return []byte(html.EscapeString(string(wild)))
	}
	well = well[i+len(openBody):]

	closeBody := []byte("</body>")
	i = bytes.Index(well, closeBody)
	if i < 0 {
		return []byte(html.EscapeString(string(wild)))
	}
	return well[:i]
}
開發者ID:eaburns,項目名稱:feedme,代碼行數:37,代碼來源:webfeed.go

示例5: TestSelectors

func TestSelectors(t *testing.T) {
	for _, test := range selectorTests {
		s, err := Compile(test.selector)
		if err != nil {
			t.Errorf("error compiling %q: %s", test.selector, err)
			continue
		}

		doc, err := html.Parse(strings.NewReader(test.HTML))
		if err != nil {
			t.Errorf("error parsing %q: %s", test.HTML, err)
			continue
		}

		matches := s.MatchAll(doc)
		if len(matches) != len(test.results) {
			t.Errorf("wanted %d elements, got %d instead", len(test.results), len(matches))
			continue
		}

		for i, m := range matches {
			got := nodeString(m)
			if got != test.results[i] {
				t.Errorf("wanted %s, got %s instead", test.results[i], got)
			}
		}
	}
}
開發者ID:jShi-git,項目名稱:goquery_sample,代碼行數:28,代碼來源:selector_test.go

示例6: ExampleParse

func ExampleParse() {
	s := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a href="/bar/baz">BarBaz</a></ul>`
	doc, err := html.Parse(strings.NewReader(s))
	if err != nil {
		log.Fatal(err)
	}
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "a" {
			for _, a := range n.Attr {
				if a.Key == "href" {
					fmt.Println(a.Val)
					break
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)
	// Output:
	// foo
	// /bar/baz
}
開發者ID:98pm,項目名稱:docker,代碼行數:25,代碼來源:example_test.go

示例7: parseStub

func parseStub(stub string) (r redditStub, err error) {
	var extract func(*html.Node)
	var doc *html.Node
	doc, err = html.Parse(strings.NewReader(stub))
	if err != nil {
		return
	}
	extract = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "a" {
			switch {
			case n.FirstChild.Data == "[link]":
				r.Link = n.Attr[0].Val
			case strings.HasSuffix(n.FirstChild.Data, " comments]"):
				r.Comments = n.Attr[0].Val
			case strings.HasPrefix(n.Attr[0].Val, "http://www.reddit.com/user/"):
				r.User = strings.TrimSpace(n.FirstChild.Data)
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			extract(c)
		}
	}
	extract(doc)
	return
}
開發者ID:hdonnay,項目名稱:dereddit,代碼行數:25,代碼來源:main.go

示例8: TestFind

func TestFind(t *testing.T) {
	s := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a class="goo" href="/bar/baz">BarBaz</a></ul>`
	doc, _ := html.Parse(strings.NewReader(s))

	_, found := Find(doc, "#foo")

	if found {
		t.Errorf("There is no node with id 'foo'")
	}

	p, found := Find(doc, "p")

	if !found || p.Data != "p" {
		t.Errorf("Couldn't find p")
	}

	a, found := Find(doc, "ul a")

	if !found || a.Data != "a" || Flatten(a) != "Foo" {
		t.Errorf("Couldn't find a")
	}

	goo, found := Find(doc, "ul .goo")

	if !found || goo.Data != "a" || Flatten(goo) != "BarBaz" {
		t.Errorf("Couldn't find a with class goo")
	}
}
開發者ID:kyleconroy,項目名稱:frantic-search,代碼行數:28,代碼來源:soup_test.go

示例9: TestFlatten

func TestFlatten(t *testing.T) {
	s := `<p>Links:</p><ul><li><a href="foo">Foo</a><li><a href="/bar/baz">BarBaz</a></ul>`
	doc, _ := html.Parse(strings.NewReader(s))
	if Flatten(doc) != "Links:FooBarBaz" {
		t.Fatalf("%s was wrong", Flatten(doc))
	}
}
開發者ID:kyleconroy,項目名稱:frantic-search,代碼行數:7,代碼來源:soup_test.go

示例10: GenerateDocument

func GenerateDocument(rawData []byte) *goquery.Document {
	utf8String := toUtf8(rawData)
	utf8byteArray := []byte(utf8String)
	node, err := html.Parse(bytes.NewReader(utf8byteArray))
	helper.HandleFatalError("document generation failed:", err)
	return goquery.NewDocumentFromNode(node)
}
開發者ID:sejoharp,項目名稱:kickerstats,代碼行數:7,代碼來源:interfaces.go

示例11: ExtractData

// Make a GET request to the given URL and start parsing
// its HTML.
func ExtractData(entity *Entity, url string) {
	// Parsing completion channel.
	done := make(chan bool, 1)

	res, err := http.Get(url)
	if err != nil {
		log.Panicln("Error requesting URL data: ", err)
	}

	defer res.Body.Close()

	doc, err := html.Parse(res.Body)
	if err != nil {
		log.Println("Error parsing URL body: ", err)
	}

	go ParseHTML(doc, entity, done)

	for {
		select {
		case <-done:
			go finalizeEntity(entity, doc, EntityDir)
		default:
		}
	}
}
開發者ID:yeah-right,項目名稱:webhog,代碼行數:28,代碼來源:parse.go

示例12: lookupTitle

func lookupTitle(url string) (title string) {
	r, err := http.Get(url)
	if err != nil {
		return "<Couldn't connect.>"
	}
	defer r.Body.Close()
	/*b, err := ioutil.ReadAll(r.Body)
	CheckError(err)
	if len(b) > 30 {
		b = b[:30]
	}
	return string(b)*/

	title = "<Untitled page.>"

	doc, err := html.Parse(r.Body)
	if err != nil {
		return "<Failed to parse HTML.>"
	}
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.DataAtom == atom.Title {
			title = extract(n)
			return
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)

	return
}
開發者ID:shurcooL-legacy,項目名稱:gophurls,代碼行數:33,代碼來源:server.go

示例13: GetStatus

func GetStatus() (Status, error) {
	resp, err := http.Get(STATUS_URL)
	if err != nil {
		log.Println(err)

		return Status{}, errors.New("Could not access OIT status page")
	}

	defer resp.Body.Close()

	doc, err := html.Parse(resp.Body)

	statusNode, err := FindStatusBlock(doc)
	if err != nil {
		log.Println(err)

		return Status{}, err
	}

	status, err := ExtractStatus(statusNode)
	if err != nil {
		log.Println(err)

		return Status{}, err
	}

	reason, err := ExtractReason(statusNode)
	if err != nil {
		log.Println(err)

		return Status{}, err
	}

	return Status{status, reason}, nil
}
開發者ID:Tanner,項目名稱:isgtwifidown.com,代碼行數:35,代碼來源:status.go

示例14: embedRedditSelf

func (e *Embedder) embedRedditSelf(url string) (rv EmbedInfo, err error) {
	matched, err := regexp.MatchString("reddit.com/r/", url)
	if err != nil {
		return
	}
	if !matched {
		err = strategyWhiffError
		return
	}
	rv.URL = url
	doc, err := goquery.NewDocument(url)
	if err != nil {
		return
	}
	doc.Find(".expando .usertext-body").Each(func(i int, s *goquery.Selection) {
		s.Find("a").Each(func(i int, s *goquery.Selection) {
			if href, ok := s.Attr("href"); ok {
				embedInfo, err := e.embedImage(href)
				if err != nil {
					return
				}
				node, err := html.Parse(strings.NewReader(embedInfo.Html))
				if err != nil {
					return
				}
				parent := s.Parent().Get(0)
				parent.RemoveChild(s.Get(0))
				parent.AppendChild(node)
			}
		})
		rv.Html, err = s.Html()
		return
	})
	return
}
開發者ID:pavelb,項目名稱:gorss,代碼行數:35,代碼來源:embed.go

示例15: GetParameters

func GetParameters(client *http.Client, site string) error {
	url, err := url.ParseRequestURI(site)
	if err != nil {
		return err
	}

	url.Path = "/parameters/profile/all"

	respBody, err := DoRequest(client, url, "GET", nil, nil)
	if err != nil {
		return err
	}

	defer respBody.Close()

	doc, err := html.Parse(respBody)
	if err != nil {
		return err
	}

	if verbose {
		fmt.Println("HTML doc parsed ok", "type:", doc.Type, "data:", doc.Data)
	}

	err = CheckHtml(doc, PARAMETERS_PAGE_TITLE)
	if err != nil {
		return err
	}

	return nil
}
開發者ID:gitvod,項目名稱:traffic_control,代碼行數:31,代碼來源:systemtest.go


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