htmlqueryvsuntangle
htmlquery is a Go library that allows you to parse and extract data from HTML documents using XPath expressions. It provides a simple and intuitive API for traversing and querying the HTML tree structure, and it is built on top of the popular Goquery library.
untangle is a simple library for parsing XML documents in Python. It allows you to access data in an XML file as if it were a Python object, making it easy to work with the data in your code.
To use untangle, you first need to install it via pip by running pip install untangle``.
Once it is installed, you can use the
untangle.parse()`` function to parse an XML file and create a Python object.
For example:
import untangle
obj = untangle.parse("example.xml")
print(obj.root.element.child)
You can also pass a file-like object or a string containing XML data to the untangle.parse() function. Once you have an untangle object, you can access elements in the XML document using dot notation.
You can also access the attributes of an element by using attrib property, eg. `obj.root.element['attrib_name']`` untangle also supports xpath-like syntax to access the elements, obj.root.xpath("path/to/element")
It also supports iteration over the elements using obj.root.element.children
for child in obj.root.element.children:
print(child)
Example Use
package main
import (
"fmt"
"log"
"github.com/antchfx/htmlquery"
)
func main() {
// Parse the HTML string
doc, err := htmlquery.Parse([]byte(`
<html>
<body>
<h1>Hello, World!</h1>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
`))
if err != nil {
log.Fatal(err)
}
// Extract the text of the first <h1> element
h1 := htmlquery.FindOne(doc, "//h1")
fmt.Println(htmlquery.InnerText(h1)) // "Hello, World!"
// Extract the text of all <li> elements
lis := htmlquery.Find(doc, "//li")
for _, li := range lis {
fmt.Println(htmlquery.InnerText(li))
}
// "Item 1"
// "Item 2"
// "Item 3"
}
import untangle
obj = untangle.parse("example.xml")
print(obj.root.element.child)
# access attributes:
print(obj.root.element['attrib_name'])
# use xpath:
element = obj.root.xpath("path/to/element")