MCPcopy
hub / github.com/D4Vinci/Scrapling / css

Method css

scrapling/parser.py:568–626  ·  view source on GitHub ↗

Search the current tree with CSS3 selectors **Important: It's recommended to use the identifier argument if you plan to use a different selector later and want to relocate the same element(s)** :param selector: The CSS3 selector to be used. :param adaptive:

(
        self,
        selector: str,
        identifier: str = "",
        adaptive: bool = False,
        auto_save: bool = False,
        percentage: int = 40,
    )

Source from the content-addressed store, hash-verified

566 return []
567
568 def css(
569 self,
570 selector: str,
571 identifier: str = "",
572 adaptive: bool = False,
573 auto_save: bool = False,
574 percentage: int = 40,
575 ) -> "Selectors":
576 """Search the current tree with CSS3 selectors
577
578 **Important:
579 It's recommended to use the identifier argument if you plan to use a different selector later
580 and want to relocate the same element(s)**
581
582 :param selector: The CSS3 selector to be used.
583 :param adaptive: Enabled will make the function try to relocate the element if it was 'saved' before
584 :param identifier: A string that will be used to save/retrieve element's data in adaptive,
585 otherwise the selector will be used.
586 :param auto_save: Automatically save new elements for `adaptive` later
587 :param percentage: The minimum percentage to accept while `adaptive` is working and not going lower than that.
588 Be aware that the percentage calculation depends solely on the page structure, so don't play with this
589 number unless you must know what you are doing!
590
591 :return: `Selectors` class.
592 """
593 if self._is_text_node(self._root):
594 return Selectors()
595
596 try:
597 if not self.__adaptive_enabled or "," not in selector:
598 # No need to split selectors in this case, let's save some CPU cycles :)
599 xpath_selector = _css_to_xpath(selector)
600 return self.xpath(
601 xpath_selector,
602 identifier or selector,
603 adaptive,
604 auto_save,
605 percentage,
606 )
607
608 results = Selectors()
609 for single_selector in split_selectors(selector):
610 # I'm doing this only so the `save` function saves data correctly for combined selectors
611 # Like using the ',' to combine two different selectors that point to different elements.
612 xpath_selector = _css_to_xpath(single_selector.canonical())
613 results += self.xpath(
614 xpath_selector,
615 identifier or single_selector.canonical(),
616 adaptive,
617 auto_save,
618 percentage,
619 )
620
621 return Selectors(results)
622 except (
623 SelectorError,
624 SelectorSyntaxError,
625 ) as e:

Calls 3

_is_text_nodeMethod · 0.95
xpathMethod · 0.95
SelectorsClass · 0.85