How to search only the first occurring item? When finding stop searching.
For example:
name = item.cssselect('span.title') Looking for spans with class title.
If we know for sure that there is only 1 element in the tree, why should we continue the search after finding the element, because time is wasted on it.
Similar to the find command in Beautiful Soup 4, which only searches for the first matching element
Search can be carried out as xpath, and cssselect.
etree.XPath(f'({xpath_expr})[1]')(item)wherexpath_expr = cssselect.HTMLTranslator().css_to_xpath(css_selector)¶ If the document is large, you can work with it in parts, for example . - jfs