Esempio n. 1
0
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
}
Esempio n. 2
0
func getAnExcuse() (*Excuse, error) {
	var endpointURL *url.URL
	endpointURL, err := url.Parse("http://www.programmerexcuses.com/")
	if err != nil {
		return nil, err
	}

	content, err := utils.GetContent(endpointURL.String(), nil)
	if err != nil {
		return nil, err
	}

	doc, err := html.Parse(strings.NewReader(string(content)))
	if err != nil {
		return nil, err
	}

	var excuseText string

	var f func(*html.Node, bool)
	f = func(n *html.Node, canExtractText bool) {
		// Extract text content if node is a TextNode
		if canExtractText && n.Type == html.TextNode {
			excuseText = n.Data
		}

		// Tell whether text content can be extracted or not.
		// Here, we will recursively traverse for 'a' ElementNode.
		canExtractText = canExtractText || (n.Type == html.ElementNode && n.Data == "a")

		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c, canExtractText)
		}
	}
	f(doc, false)

	return &Excuse{Text: excuseText, Response: "true"}, err
}