# Parsing main classes 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 ``` Usage: ```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. The main page is a [Selector](#selector) object, and the elements within are [Selector](#selector) objects. Any text (text content inside elements or attribute values) is a [TextHandler](#texthandler) object, and element attributes are stored as [AttributesHandler](#attributeshandler). ## 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`. The arguments `url`, `adaptive`, `storage`, and `storage_args` are settings used with the `adaptive` feature. They are explained in the [adaptive](adaptive.md) feature page. Arguments for parsing adjustments: - **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. The arguments `huge_tree` and `root` are advanced features not covered here. Most properties on the main page and its elements are lazily loaded (not initialized until accessed), which contributes to Scrapling's speed. ### Properties Properties for traversal are separated in the [traversal](#traversal) section below. Parsing this HTML page as an example: ```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 (used as an example throughout): ```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 The text returned is a [TextHandler](#texthandler), not a standard string. 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' ``` Using it on the page directly operates on the root `html` element: ```python >>> page.tag 'html' ``` 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 Properties and methods for navigating elements on the page. The `html` element is the root of the website's tree. Elements like `head` and `body` are "children" of `html`, and `html` is their "parent". The element `body` is a "sibling" of `head` and vice versa. Accessing the parent of an element ```python >>> article.parent
>>> article.parent.tag 'div' ``` Chaining is supported, as with all similar properties/methods: ```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='
,