Product 1
This is product 1
$10.99# 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
This is product 1
$10.99This is product 2
$20.99This is product 3
$15.99This is product 1
\n $10.99\nThis is product 1
$10.99{"some_key": "some_value"}
' ``` The `json` method can be used directly: ```python >>> page.json() {'some_key': 'some_value'} ``` For JSON responses, the [Selector](#selector) class keeps a raw copy of the content it receives. When `.json()` is called, it checks for that raw copy first and converts it to JSON. If the raw copy is unavailable (as with sub-elements), it checks the current element's text content, then falls back to `get_all_text`. - The `.clean()` method removes all whitespace and consecutive spaces, returning a new `TextHandler` instance: ```python >>> TextHandler('\n wonderful idea, \reh?').clean() 'wonderful idea, eh?' ``` The `remove_entities` argument causes `clean` to replace HTML entities with their corresponding characters. - The `.sort()` method sorts the string characters: ```python >>> TextHandler('acb').sort() 'abc' ``` Or do it in reverse: ```python >>> TextHandler('acb').sort(reverse=True) 'cba' ``` This class is returned in place of strings nearly everywhere in the library. ## TextHandlers This class inherits from standard lists, adding `re` and `re_first` as new methods. The `re_first` method runs `re` on each [TextHandler](#texthandler) and returns the first result, or `None`. ## AttributesHandler This is a read-only version of Python's standard dictionary, or `dict`, used solely to store the attributes of each element/[Selector](#selector) instance. ```python >>> print(page.find('script').attrib) {'id': 'page-data', 'type': 'application/json'} >>> type(page.find('script').attrib).__name__ 'AttributesHandler' ``` Because it's read-only, it will use fewer resources than the standard dictionary. Still, it has the same dictionary method and properties, except those that allow you to modify/override the data. It currently adds two extra simple methods: - The `search_values` method Searches the current attributes by values (rather than keys) and returns a dictionary of each matching item. A simple example would be ```python >>> for i in page.find('script').attrib.search_values('page-data'): print(i) {'id': 'page-data'} ``` But this method provides the `partial` argument as well, which allows you to search by part of the value: ```python >>> for i in page.find('script').attrib.search_values('page', partial=True): print(i) {'id': 'page-data'} ``` A more practical example is using it with `find_all` to find all elements that have a specific value in their attributes: ```python >>> page.find_all(lambda element: list(element.attrib.search_values('product'))) [