goqueryvsselectolax
goquery brings a syntax and a set of features similar to jQuery to the Go language. goquery is a popular and easy-to-use library for Go that allows you to use a CSS selector-like syntax to select elements from an HTML document.
It is based on Go's net/html package and the CSS Selector library cascadia. Since the net/html parser returns nodes, and not a full-featured DOM tree, jQuery's stateful manipulation functions (like height(), css(), detach()) have been left off.
Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML. See the wiki for various options to do this. Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface. jQuery being the ultra-popular library that it is, I felt that writing a similar HTML-manipulating library was better to follow its API than to start anew (in the same spirit as Go's fmt package), even though some of its methods are less than intuitive (looking at you, index()...).
goquery can download HTML by itself (using built-in http client) though it's not recommended for web scraping as it's likely to be blocked.
selectolax is a fast and lightweight library for parsing HTML and XML documents in Python. It is designed to be a drop-in replacement for the popular BeautifulSoup library, with significantly faster performance.
selectolax uses a Cython-based parser to quickly parse and navigate through HTML and XML documents. It provides a simple and intuitive API for working with the document's structure, similar to BeautifulSoup.
To use selectolax, you first need to install it via pip by running pip install selectolax``.
Once it is installed, you can use the
selectolax.html.fromstring()function to parse an HTML document and create a selectolax object.
For example:
selectolax.html.fromstring()from selectolax.parser import HTMLParser
html_string = "<html><body>Hello, World!</body></html>"
root = HTMLParser(html_string).root
print(root.tag) # html
with file-like objects, bytes or file paths,
as well as
selectolax.xml.fromstring()`` for parsing XML documents.
Once you have a selectolax object, you can use the select()
method to search for elements in the document using CSS selectors,
similar to BeautifulSoup. For example:
body = root.select("body")[0]
print(body.text()) # "Hello, World!"
Like BeautifulSoups find
and find_all
methods selectolax also supports searching using the search()`` method, which returns the first matching element,
and the
search_all()`` method, which returns all matching elements.
Example Use
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
)
func main() {
// Use goquery.NewDocument to load an HTML document
// This can load from URL
doc, err := goquery.NewDocument("http://example.com")
// or HTML string:
doc, err := goquery.NewDocumentFromReader("some html")
if err != nil {
fmt.Println("Error:", err)
return
}
// Use the Selection.Find method to select elements from the document
doc.Find("a").Each(func(i int, s *goquery.Selection) {
// Use the Selection.Text method to get the text of the element
fmt.Println(s.Text())
// Use the Selection.Attr method to get the value of an attribute
fmt.Println(s.Attr("href"))
})
}
from selectolax.parser import HTMLParser
html_string = "<html><body>Hello, World!</body></html>"
root = HTMLParser(html_string).root
print(root.tag) # html
# use css selectors:
body = root.select("body")[0]
print(body.text()) # "Hello, World!"
# find first matching element:
body = root.search("body")
print(body.text()) # "Hello, World!"
# or all matching elements:
html_string = "<html><body><p>paragraph1</p><p>paragraph2</p></body></html>"
root = HTMLParser(html_string).root
for el in root.search_all("p"):
print(el.text())
# will print:
# paragraph 1
# paragraph 2