day7函数 练习5.8
							parent
							
								
									23d290d7b8
								
							
						
					
					
						commit
						d33b37a04a
					
				|  | @ -0,0 +1,101 @@ | ||||||
|  | package main | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"Study/src/study/day7Function/HtmlTools" | ||||||
|  | 	"fmt" | ||||||
|  | 	"golang.org/x/net/html" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func main() { | ||||||
|  | 	// 定义变量,类型为函数func(n *html.Node, pre func(doc *html.Node, id string) *html.Node, id string)
 | ||||||
|  | 	// pre 参数规定了必须传入一个函数类型的变量,
 | ||||||
|  | 	// 有两个参数,doc和id, 返回值类型为 *html.Node
 | ||||||
|  | 	//var forEachNode func(n *html.Node, pre func(doc *html.Node, id string) *html.Node, id string) *html.Node
 | ||||||
|  | 	//// 实现此函数
 | ||||||
|  | 	//forEachNode = func(n *html.Node, pre func(doc *html.Node, id string) *html.Node, id string) *html.Node {
 | ||||||
|  | 	//	c := n.FirstChild
 | ||||||
|  | 	//	if pre != nil && pre(n, id) != nil {
 | ||||||
|  | 	//		fmt.Println(n.Data)
 | ||||||
|  | 	//		return c
 | ||||||
|  | 	//	}
 | ||||||
|  | 	//	for ; c != nil; c = c.NextSibling {
 | ||||||
|  | 	//		// 递归调用,逐级遍历
 | ||||||
|  | 	//		forEachNode(c, pre, id)
 | ||||||
|  | 	//	}
 | ||||||
|  | 	//	return nil
 | ||||||
|  | 	//}
 | ||||||
|  | 	//// 定义需要传入的函数
 | ||||||
|  | 	//var ElementByID func(doc *html.Node, id string) *html.Node
 | ||||||
|  | 	//ElementByID = func(doc *html.Node, id string) *html.Node {
 | ||||||
|  | 	//	if doc == nil {
 | ||||||
|  | 	//		return nil
 | ||||||
|  | 	//	} else if doc.Type == html.ElementNode && doc.Data == id {
 | ||||||
|  | 	//		fmt.Println(doc.Data)
 | ||||||
|  | 	//		return doc
 | ||||||
|  | 	//	}
 | ||||||
|  | 	//	return nil
 | ||||||
|  | 	//}
 | ||||||
|  | 	////调用
 | ||||||
|  | 	//forEachNode(HtmlTools.GetHtml(), ElementByID, "a")
 | ||||||
|  | 
 | ||||||
|  | 	var each func(doc *html.Node) func(id string) *html.Node | ||||||
|  | 	each = func(doc *html.Node) func(id string) *html.Node { | ||||||
|  | 		if doc == nil { | ||||||
|  | 			return func(id string) *html.Node { | ||||||
|  | 				fmt.Println("0") | ||||||
|  | 				if doc.Type == html.ElementNode && doc.Data == id { | ||||||
|  | 					fmt.Print(doc.Data) | ||||||
|  | 					ResultNode = doc | ||||||
|  | 					fmt.Println("ret: id") | ||||||
|  | 					return ResultNode | ||||||
|  | 				} | ||||||
|  | 				return ResultNode | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		each(doc.FirstChild) | ||||||
|  | 		each(doc.NextSibling) | ||||||
|  | 		return func(id string) *html.Node { | ||||||
|  | 			fmt.Println("1") | ||||||
|  | 			if doc.Type == html.ElementNode && doc.Data == id { | ||||||
|  | 				fmt.Print("data", doc.Data) | ||||||
|  | 				ResultNode = doc | ||||||
|  | 				fmt.Println("ret: id") | ||||||
|  | 				return ResultNode | ||||||
|  | 			} | ||||||
|  | 			fmt.Println("4") | ||||||
|  | 			return ResultNode | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	//each1 := each(HtmlTools.GetHtml())
 | ||||||
|  | 	//each1("a")
 | ||||||
|  | 
 | ||||||
|  | 	// 手动清除ResultNode的内容
 | ||||||
|  | 	ResultNode = nil | ||||||
|  | 	ElementByID(HtmlTools.GetHtml(), pre, "a") | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var ResultNode *html.Node = nil | ||||||
|  | 
 | ||||||
|  | func ElementByID(doc *html.Node, pre func(n *html.Node, id *string) bool, id string) *html.Node { | ||||||
|  | 
 | ||||||
|  | 	if doc != nil && ResultNode == nil { | ||||||
|  | 		ElementByID(doc.FirstChild, pre, id) | ||||||
|  | 		ElementByID(doc.NextSibling, pre, id) | ||||||
|  | 	} else { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if pre(doc, &id) { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | func pre(n *html.Node, id *string) bool { | ||||||
|  | 	if n.Type == html.ElementNode && n.Data == *id { | ||||||
|  | 		fmt.Println(n.Data) | ||||||
|  | 		ResultNode = n | ||||||
|  | 		return true | ||||||
|  | 	} | ||||||
|  | 	return false | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue