# Querying `minestrone` allows searching through HTML via CSS selectors (similar to JQuery or other frontend libraries). ```{note} Querying uses the [`select`](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors) method in `Beautiful Soup` which delegates to `SoupSieve`. More details about `SoupSieve` is available in [their documentation](https://facelessuser.github.io/soupsieve/). ``` ## root_element Gets the root [element](element.md) of the HTML. ```python from minestrone import HTML html = HTML("""
Dormouse
""") assert html.root_element.name == "div" ``` ## elements Recursively get all [elements](element.md) in the HTML. ```python from minestrone import HTML html = HTML("""
Dormouse
""") assert [e.name for e in html.elements] == ["div", "span"] ``` ## query Takes a CSS selector and returns an iterator of [`Element`](element.md) items. ### Query by element name ```python from minestrone import HTML html = HTML("""

The Dormouse's Story

There was a table...

""") for h1 in html.query("h1"): assert str(h1) == "

The Dormouse's Story

" ``` ### Query by id ```python from minestrone import HTML html = HTML(""" """) for a in html.query("a#elsie"): assert str(a) == 'Elsie' ``` ### Query by class ```python from minestrone import HTML html = HTML(""" """) elsie_link = next(html.query("ul li a.sister")) assert str(elsie_link) == 'Elsie' lacie_link = next(html.query("ul li a.sister")) assert str(lacie_link) == 'Lacie' ``` ## query_to_list Exactly the same as [query](querying.md#query) except it returns a list of [`Element`](element.md) items instead of a generator. This is sometimes more useful than the `query` above, but it can take more time to parse and more memory to store the data if the HTML document is large. ```python from minestrone import HTML html = HTML(""" """) assert len(html.query_to_list("a")) == 2 assert str(html.query_to_list("a")[0]) == 'Elsie' assert html.query_to_list("a") == list(html.query("a")) ```