# Parsing main classes !!! success "Prerequisites" - You’ve completed or read the [Querying elements](../parsing/selection.md) page to understand how to find/extract elements from the [Selector](../parsing/main_classes.md#selector) object. After exploring the various ways to select elements with Scrapling and its related features, let's take a step back and examine the [Selector](#selector) class in general, as well as other objects, to gain a better understanding of the parsing engine. The [Selector](#selector) class is the core parsing engine in Scrapling, providing HTML parsing and element selection capabilities. You can always import it with any of the following imports ```python from scrapling import Selector from scrapling.parser import Selector ``` Then use it directly as you already learned in the [overview](../overview.md) page ```python page = Selector( '...', url='https://example.com' ) # Then select elements as you like elements = page.css('.product') ``` In Scrapling, the main object you deal with after passing an HTML source or fetching a website is, of course, a [Selector](#selector) object. Any operation you do, like selection, navigation, etc., will return either a [Selector](#selector) object or a [Selectors](#selectors) object, given that the result is element/elements from the page, not text or similar. In other words, the main page is a [Selector](#selector) object, and the elements within are [Selector](#selector) objects, and so on. Any text, such as the text content inside elements or the text inside element attributes, is a [TextHandler](#texthandler) object, and the attributes of each element are stored as [AttributesHandler](#attributeshandler). We will return to both objects later, so let's focus on the [Selector](#selector) object. ## Selector ### Arguments explained The most important one is `content`, it's used to pass the HTML code you want to parse, and it accepts the HTML content as `str` or `bytes`. Otherwise, you have the arguments `url`, `adaptive`, `storage`, and `storage_args`. All these arguments are settings used with the `adaptive` feature, and they don't make a difference if you are not going to use that feature, so just ignore them for now, and we will explain them in the [adaptive](adaptive.md) feature page. Then you have the arguments for parsing adjustments or adjusting/manipulating the HTML content while the library is parsing it: - **encoding**: This is the encoding that will be used while parsing the HTML. The default is `UTF-8`. - **keep_comments**: This tells the library whether to keep HTML comments while parsing the page. It's disabled by default because it can cause issues with your scraping in various ways. - **keep_cdata**: Same logic as the HTML comments. [cdata](https://stackoverflow.com/questions/7092236/what-is-cdata-in-html) is removed by default for cleaner HTML. I have intended to ignore the arguments `huge_tree` and `root` to avoid making this page more complicated than needed. You may notice that I'm doing that a lot because it involves advanced features that you don't need to know to use the library. The development section will cover these missing parts if you are very invested. After that, most properties on the main page and its elements are lazily loaded. This means they don't get initialized until you use them like the text content of a page/element, and this is one of the reasons for Scrapling speed :) ### Properties You have already seen much of this on the [overview](../overview.md) page, but don't worry if you didn't. We will review it more thoroughly using more advanced methods/usages. For clarity, the properties for traversal are separated below in the [traversal](#traversal) section. Let's say we are parsing this HTML page for simplicity: ```html Some page

Product 1

This is product 1

$10.99

Product 2

This is product 2

$20.99

Product 3

This is product 3

$15.99
``` Load the page directly as shown before: ```python from scrapling import Selector page = Selector(html_doc) ``` Get all text content on the page recursively ```python >>> page.get_all_text() 'Some page\n\n \n\n \nProduct 1\nThis is product 1\n$10.99\nIn stock: 5\nProduct 2\nThis is product 2\n$20.99\nIn stock: 3\nProduct 3\nThis is product 3\n$15.99\nOut of stock' ``` Get the first article, as explained before; we will use it as an example ```python article = page.find('article') ``` With the same logic, get all text content on the element recursively ```python >>> article.get_all_text() 'Product 1\nThis is product 1\n$10.99\nIn stock: 5' ``` But if you try to get the direct text content, it will be empty because it doesn't have direct text in the HTML code above ```python >>> article.text '' ``` The `get_all_text` method has the following optional arguments: 1. **separator**: All strings collected will be concatenated using this separator. The default is '\n'. 2. **strip**: If enabled, strings will be stripped before concatenation. Disabled by default. 3. **ignore_tags**: A tuple of all tag names you want to ignore in the final results and ignore any elements nested within them. The default is `('script', 'style',)`. 4. **valid_values**: If enabled, the method will only collect elements with real values, so all elements with empty text content or only whitespaces will be ignored. It's enabled by default By the way, the text returned here is not a standard string but a [TextHandler](#texthandler); we will get to this in detail later, so if the text content can be serialized to JSON, use `.json()` on it ```python >>> script = page.find('script') >>> script.json() {'lastUpdated': '2024-09-22T10:30:00Z', 'totalProducts': 3} ``` Let's continue to get the element tag ```python >>> article.tag 'article' ``` If you use it on the page directly, you will find that you are operating on the root `html` element ```python >>> page.tag 'html' ``` Now, I think I've hammered the (`page`/`element`) idea, so I won't return to it. Getting the attributes of the element ```python >>> print(article.attrib) {'class': 'product', 'data-id': '1'} ``` Access a specific attribute with any of the following ```python >>> article.attrib['class'] >>> article.attrib.get('class') >>> article['class'] # new in v0.3 ``` Check if the attributes contain a specific attribute with any of the methods below ```python >>> 'class' in article.attrib >>> 'class' in article # new in v0.3 ``` Get the HTML content of the element ```python >>> article.html_content '

Product 1

\n

This is product 1

\n $10.99\n \n
' ``` Get the prettified version of the element's HTML content ```python print(article.prettify()) ``` ```html

Product 1

This is product 1

$10.99
``` Use the `.body` property to get the raw content of the page. Starting from v0.4, when used on a `Response` object from fetchers, `.body` always returns `bytes`. ```python >>> page.body '\n \n Some page\n \n ...' ``` To get all the ancestors in the DOM tree of this element ```python >>> article.path [
,
, Some page] ``` Generate a CSS shortened selector if possible, or generate the full selector ```python >>> article.generate_css_selector 'body > div > article' >>> article.generate_full_css_selector 'body > div > article' ``` Same case with XPath ```python >>> article.generate_xpath_selector "//body/div/article" >>> article.generate_full_xpath_selector "//body/div/article" ``` ### Traversal Using the elements we found above, we will go over the properties/methods for moving on the page in detail. If you are unfamiliar with the DOM tree or the tree data structure in general, the following traversal part can be confusing. I recommend you look up these concepts online to better understand them. If you are too lazy to search about it, here's a quick explanation to give you a good idea.
In simple words, the `html` element is the root of the website's tree, as every page starts with an `html` element.
This element will be positioned directly above elements such as `head` and `body`. These are considered "children" of the `html` element, and the `html` element is considered their "parent". The element `body` is a "sibling" of the element `head` and vice versa. Accessing the parent of an element ```python >>> article.parent
>>> article.parent.tag 'div' ``` You can chain it as you want, which applies to all similar properties/methods we will review. ```python >>> article.parent.parent.tag 'body' ``` Get the children of an element ```python >>> article.children [Product 1' parent='
, This is product 1...' parent='
, $10.99' parent='
,