HTML解析库:goquery
goquery是一个使用go语言写成的HTML解析库,可以让你像jQuery那样的方式来操作DOM文档。下面是示例:
func ExampleScrape_MetalReview() { // Load the HTML document (in real use, the type would be *goquery.Document) var doc *Document var e error if doc, e = NewDocument("http://metalreview.com"); e != nil { panic(e.Error()) } // Find the review items (the type of the Selection would be *goquery.Selection) doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) { var band, title string var score float64 // For each item found, get the band, title and score, and print it band = s.Find("strong").Text() title = s.Find("em").Text() if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil { // Not a valid float, ignore score fmt.Printf("Review %d: %s - %s.\n", i, band, title) } else { // Print all, including score fmt.Printf("Review %d: %s - %s (%2.1f).\n", i, band, title, score) } })
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!