Adding css_first and xpath_first for easier usage

This commit is contained in:
Karim shoair
2024-11-05 23:41:51 +02:00
parent 009c2914aa
commit 3622040440
2 changed files with 55 additions and 3 deletions
+3 -3
View File
@@ -59,7 +59,7 @@ quotes = page.css('.quote').css('.text::text') # Chained selectors
quotes = [element.text for element in page.css('.quote').css('.text')] # Slower than bulk query above
# Get the first quote element
quote = page.css('.quote').first # or [0] or .get()
quote = page.css_first('.quote') # or page.css('.quote').first or [0] or .get()
# Working with elements
quote.html_content # Inner HTML
@@ -244,8 +244,8 @@ To increase the complexity a little bit, let's say we want to get all books' dat
```python
>>> for product in page.find_by_text('Tipping the Velvet').parent.parent.find_similar():
print({
"name": product.css('h3 a::text')[0],
"price": product.css('.price_color')[0].re_first(r'[\d\.]+'),
"name": product.css_first('h3 a::text'),
"price": product.css_first('.price_color').re_first(r'[\d\.]+'),
"stock": product.css('.availability::text')[-1].clean()
})
{'name': 'A Light in the ...', 'price': '51.77', 'stock': 'In stock'}