## Introduction After exploring the various ways to select elements with Scrapling and related features, Let's take a step back and examine the [Adaptor](#adaptor) class generally and other objects to better understand the parsing engine. The [Adaptor](#adaptor) class is the core parsing engine in Scrapling that provides HTML parsing and element selection capabilities. You can always import it with any of the following imports ```python from scrapling import Adaptor from scrapling.parser import Adaptor ``` then use it directly as you already learned in the [overview](../overview.md) page ```python adaptor = Adaptor( text='...', url='https://example.com' ) # Then select elements as you like elements = adaptor.css('.product') ``` In Scrapling, the main object you deal with after passing an HTML source or fetching a website is, of course, an [Adaptor](#adaptor) object. Any operation you do, like selection, navigation, etc., will return either an [Adaptor](#adaptor) object or an [Adaptors](#adaptors) object, given that the result is element/elements from the page, not text or similar. In other words, the main page is a [Adaptor](#adaptor) object, and the elements within are [Adaptor](#adaptor) 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 [Adaptor](#adaptor) object. ## Adaptor ### Arguments explained The most important ones are `text` and `body`. Both are used to pass the HTML code you want to parse, but the first one accepts `str`, and the latter accepts `bytes` like how you used to do with `parsel` :) Otherwise, you have the arguments `url`, `auto_match`, `storage`, and `storage_args`. All these arguments are settings used with the `auto_match` 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 [automatch](automatch.md) feature page. Then you have the arguments for adjustments for parsing or adjusting/manipulating the HTML while the library 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, as it can mess up your scraping in many 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. This also means when you check for the raw html content, you will find it doesn't have the cdata. 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, and that's because it's something you don't need to know to use the library. The development section will cover these missing parts if you are that interested. After that, for the main page and elements within, most properties don't get initialized until you use it 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 Adaptor page = Adaptor(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; notice the logic difference ```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. 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, then 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 used it on the page directly, you will find you are operating on the root `html` element ```python >>> page.tag 'html' ``` Now, I think I hammered the (`page`/`element`) idea, so I won't return to it again. Getting the attributes of the element ```python >>> print(article.attrib) {'class': 'product', 'data-id': '1'} ``` Get the HTML content of the element ```python >>> article.html_content '

Product 1

\n

This is product 1

\n $10.99\n \n
' ``` It's the same if you used the `.body` property ```python >>> article.body '

Product 1

\n

This is product 1

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

Product 1

This is product 1

$10.99
``` 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 in 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 for a better understanding. If you are too lazy to search about it, here's a quick explanation to give you a good idea.
Simply put, the `html` element is the root of the website's tree, as every page starts with an `html` element.
This element will be directly above elements like `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='
,