

goquery - Iterate over All DOM Elements in HTML
source link: http://siongui.github.io/2016/05/21/goquery-iterate-all-nodes/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introduction
Iterate over all DOM nodes/elements via goquery in Golang (Go programming language).
The trick is to use Find("*") to access all nodes in DOM tree.
Install goquery Package
$ go get -u github.com/PuerkitoBio/goquery
Source Code
node.go | repository | view raw
package main import ( "github.com/PuerkitoBio/goquery" "strings" ) const html = `<html> <head> <title>traverse</title> </head> <body> <div> Hello <span>World</span> <!-- Goquery --> </div> </body> </html>` func main() { doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) if err != nil { panic(err) } doc.Find("*").Each(func(_ int, node *goquery.Selection) { println(node.Text()) }) }
Tested on: Ubuntu Linux 16.04, Go 1.6.2.
References:
Recommend
-
9
3 Ways to Select HTML DOM Elements Navigating the HTML document object model (DOM) of a web app used to be one of the more tedious tasks of a front-end developer. With the increased usage of modern frameworks like
-
4
how to iterate on all specified hosts advertisements I have a playbook that is supposed to create a config file for all specified hosts, on my...
-
11
Introduction Iterate over all DOM elements in HTML via Golang. Use
-
5
[Golang] Fetch DOM Style Object Properties via goquery February 28, 2017 ...
-
9
goquery Example - Get Simple Text From HTML Page April 17, 2016 ...
-
11
goquery Handle Non-UTF8 HTML Web Page October 09, 2018
-
6
[Golang] Get HTML Title via goquery March 22, 2016 A simple example to read HT...
-
5
Introduction Convert all HTML links to restructuredtext in a webpage via
-
10
HTML table to reStructuredText list-...
-
2
Install goquery Package $ go get -u github.com/PuerkitoBio/goquery Source Code
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK