Exemple #1
0
// Asserts that the TestResponse contains a CSS-style selector.
// This method supports the SelectorQuery style selectors from
// go-html-transform, but accepts them as a single string with
// space delimiters
func (r *TestResponse) AssertSelector(selector string, t *testing.T) {
	body, e := ioutil.ReadAll(r.Response.Body)

	if e != nil {
		t.Errorf("Response not readable: %s", r.Url)
	}

	doc, e := transform.NewDoc(string(body))

	if e != nil {
		t.Errorf("Could not parse response: %s", r.Url)
	}

	selector_parts := strings.Split(selector, " ")

	q := make(transform.SelectorQuery, len(selector_parts))

	for i, str := range selector_parts {
		selector := transform.NewSelector(str)

		if selector == nil {
			t.Errorf("Problem with selector: %s", str)
		}

		q[i] = selector
	}

	if len(q.Apply(doc)) == 0 {
		t.Errorf("Selector not found: %s", selector)
	}
}
Exemple #2
0
func parseLinks(content string) (urls []string, err error) {
	doc, err := transform.NewDoc(content)
	if err == nil {
		selector := transform.NewSelectorQuery("a")
		nodes := selector.Apply(doc)
		for i := 0; i < len(nodes); i++ {
			for j := 0; j < len(nodes[i].Attr); j++ {
				if nodes[i].Attr[j].Name == "href" && strings.HasPrefix(nodes[i].Attr[j].Value, "http") {
					urls = append(urls, nodes[i].Attr[j].Value)
				}
			}
		}
	}
	return urls, err
}
//Get the names of people who commented on a pin
func getPinCommenters(html string) []string {
	doc, _ := transform.NewDoc(html)
	selector := transform.NewSelectorQuery("div.comment convo clearfix", "p", "a")

	//Find all the dom nodes that match the selector above
	nodes := selector.Apply(doc)

	names := []string{}
	for i := range nodes {
		//The first child of the node will be the TextNode (the content)
		//of the tag. This contains the name we want so append to our output array
		names = append(names, nodes[i].Children[0].String())
	}

	return names
}
//Get the names of people who created a pin
func getPinners(html string) []string {
	doc, _ := transform.NewDoc(html)
	selector := transform.NewSelectorQuery("div.convo attribution clearfix", "p")

	//Find all the dom nodes that match the selector above
	nodes := selector.Apply(doc)

	names := []string{}
	for i := range nodes {
		//The name we are looking for is in a different place for the pinner
		//than in the above function (this was found by just looking at the source generated
		//for the pinterest front page)
		names = append(names, nodes[i].Children[1].Children[0].String())
	}

	return names
}