,
...]
```
+You can safely access the first or last element without worrying about index errors:
+```python
+>>> page.css('.product').first # First Selector or None
+
+>>> page.css('.product').last # Last Selector or None
+
+>>> page.css('.nonexistent').first # Returns None instead of raising IndexError
+```
+
If you are too lazy like me and want to know the number of [Selector](#selector) instances in a [Selectors](#selectors) instance. You can do this:
```python
page.css('.product_pod').length
@@ -440,14 +479,14 @@ First, we start with the `re` and `re_first` methods. These are the same methods
- You also have the `.json()` method, which tries to convert the content to a JSON object quickly if possible; otherwise, it throws an error
```python
- >>> page.css_first('#page-data::text')
+ >>> page.css('#page-data::text').get()
'\n {\n "lastUpdated": "2024-09-22T10:30:00Z",\n "totalProducts": 3\n }\n '
- >>> page.css_first('#page-data::text').json()
+ >>> page.css('#page-data::text').get().json()
{'lastUpdated': '2024-09-22T10:30:00Z', 'totalProducts': 3}
```
Hence, if you didn't specify a text node while selecting an element (like the text content or an attribute text content), the text content will be selected automatically, like this
```python
- >>> page.css_first('#page-data').json()
+ >>> page.css('#page-data')[0].json()
{'lastUpdated': '2024-09-22T10:30:00Z', 'totalProducts': 3}
```
The [Selector](#selector) class adds one thing here, too; let's say this is the page we are working with:
@@ -468,12 +507,12 @@ First, we start with the `re` and `re_first` methods. These are the same methods
The [Selector](#selector) class has the `get_all_text` method, which you should be aware of by now. This method returns a `TextHandler`, of course.
So, as you know here, if you did something like this
```python
- >>> page.css_first('div::text').json()
+ >>> page.css('div::text').get().json()
```
You will get an error because the `div` tag doesn't have any direct text content that can be serialized to JSON; it doesn't have any direct text content at all.
In this case, the `get_all_text` method comes to the rescue, so you can do something like that
```python
- >>> page.css_first('div').get_all_text(ignore_tags=[]).json()
+ >>> page.css('div')[0].get_all_text(ignore_tags=[]).json()
{'lastUpdated': '2024-09-22T10:30:00Z', 'totalProducts': 3}
```
I used the `ignore_tags` argument here because the default value of it is `('script', 'style',)`, as you are aware.