How to use the cssselect.xpath.ExpressionError function in cssselect

To help you get started, we’ve selected a few cssselect examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scrapy / scrapy / tests / test_selector_csstranslator.py View on Github external
def test_unknown_pseudo_class(self):
        cases = [
            (':text', ExpressionError),
            (':attribute(name)', ExpressionError),
        ]
        for css, exc in cases:
            self.assertRaises(exc, self.c2x, css)
github scrapy / parsel / tests / test_selector_csstranslator.py View on Github external
def test_pseudo_function_exception(self):
        cases = [
            ('::attribute(12)', ExpressionError),
            ('::text()', ExpressionError),
            ('::attr(@href)', SelectorSyntaxError),
        ]
        for css, exc in cases:
            self.assertRaises(exc, self.c2x, css)
github gawel / pyquery / pyquery / cssselectpatch.py View on Github external
def xpath_lt_function(self, xpath, function):
        """Matches all elements with an index below the given one::

            >>> from pyquery import PyQuery
            &gt;&gt;&gt; d = PyQuery('<div><h1 class="first"></h1><h1 class="last"></h1></div>')
            &gt;&gt;&gt; d('h1:lt(1)')
            []

        ..
        """
        if function.argument_types() != ['NUMBER']:
            raise ExpressionError(
                "Expected a single integer for :gt(), got %r" % (
                    function.arguments,))

        value = int(function.arguments[0].value)
        xpath.add_post_condition('position() &lt; %s' % (value + 1))
        return xpath
github scrapy / scrapy / scrapy / selector / csssel.py View on Github external
def xpath_attr_functional_pseudo_element(self, xpath, function):
        if function.argument_types() not in (['STRING'], ['IDENT']):
            raise ExpressionError(
                "Expected a single string or ident for ::attr(), got %r"
                % function.arguments)
        return ScrapyXPathExpr.from_xpath(xpath,
            attribute=function.arguments[0].value)
github scrapy / parsel / parsel / csstranslator.py View on Github external
def xpath_pseudo_element(self, xpath, pseudo_element):
        """
        Dispatch method that transforms XPath to support pseudo-element
        """
        if isinstance(pseudo_element, FunctionalPseudoElement):
            method = 'xpath_%s_functional_pseudo_element' % (
                pseudo_element.name.replace('-', '_'))
            method = _unicode_safe_getattr(self, method, None)
            if not method:
                raise ExpressionError(
                    "The functional pseudo-element ::%s() is unknown"
                    % pseudo_element.name)
            xpath = method(xpath, pseudo_element)
        else:
            method = 'xpath_%s_simple_pseudo_element' % (
                pseudo_element.replace('-', '_'))
            method = _unicode_safe_getattr(self, method, None)
            if not method:
                raise ExpressionError(
                    "The pseudo-element ::%s is unknown"
                    % pseudo_element)
            xpath = method(xpath)
        return xpath
github gawel / pyquery / pyquery / cssselectpatch.py View on Github external
def xpath_gt_function(self, xpath, function):
        """Matches all elements with an index over the given one::

            &gt;&gt;&gt; from pyquery import PyQuery
            &gt;&gt;&gt; d = PyQuery('<div><h1 class="first"></h1><h1 class="last"></h1></div>')
            &gt;&gt;&gt; d('h1:gt(0)')
            []

        ..
        """
        if function.argument_types() != ['NUMBER']:
            raise ExpressionError(
                "Expected a single integer for :gt(), got %r" % (
                    function.arguments,))
        value = int(function.arguments[0].value)
        xpath.add_post_condition('position() &gt; %s' % (value + 1))
        return xpath
github mcs07 / ChemDataExtractor / chemdataextractor / scrape / csstranslator.py View on Github external
def xpath_attr_functional_pseudo_element(self, xpath, function):
        if function.argument_types() not in (['STRING'], ['IDENT']):
            raise ExpressionError("Expected a single string or ident for ::attr(), got %r" % function.arguments)
        return CdeXPathExpr.from_xpath(xpath, attribute=function.arguments[0].value)
github gawel / pyquery / pyquery / cssselectpatch.py View on Github external
def xpath_contains_function(self, xpath, function):
        """Matches all elements that contain the given text

            &gt;&gt;&gt; from pyquery import PyQuery
            &gt;&gt;&gt; d = PyQuery('<div><h1></h1><h1 class="title">title</h1></div>')
            &gt;&gt;&gt; d('h1:contains("title")')
            []

        ..
        """
        if function.argument_types() not in (['STRING'], ['IDENT']):
            raise ExpressionError(
                "Expected a single string or ident for :contains(), got %r" % (
                    function.arguments,))

        value = self.xpath_literal(function.arguments[0].value)
        xpath.add_post_condition('contains(., %s)' % value)
        return xpath
github scrapy / parsel / parsel / csstranslator.py View on Github external
def xpath_attr_functional_pseudo_element(self, xpath, function):
        """Support selecting attribute values using ::attr() pseudo-element
        """
        if function.argument_types() not in (['STRING'], ['IDENT']):
            raise ExpressionError(
                "Expected a single string or ident for ::attr(), got %r"
                % function.arguments)
        return XPathExpr.from_xpath(xpath,
                                    attribute=function.arguments[0].value)
github scrapy / scrapy / scrapy / selector / csstranslator.py View on Github external
def xpath_pseudo_element(self, xpath, pseudo_element):
        if isinstance(pseudo_element, FunctionalPseudoElement):
            method = 'xpath_%s_functional_pseudo_element' % (
                pseudo_element.name.replace('-', '_'))
            method = _unicode_safe_getattr(self, method, None)
            if not method:
                raise ExpressionError(
                    "The functional pseudo-element ::%s() is unknown"
                % pseudo_element.name)
            xpath = method(xpath, pseudo_element)
        else:
            method = 'xpath_%s_simple_pseudo_element' % (
                pseudo_element.replace('-', '_'))
            method = _unicode_safe_getattr(self, method, None)
            if not method:
                raise ExpressionError(
                    "The pseudo-element ::%s is unknown"
                    % pseudo_element)
            xpath = method(xpath)
        return xpath