当前位置: 首页>>代码示例>>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;未经允许,请勿转载。