How to use the pyquery.cssselectpatch.selector_to_xpath function in pyquery

To help you get started, we’ve selected a few pyquery 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 dsc / pyquery / pyquery / pyquery.py View on Github external
def find(self, selector):
        """Find elements using selector traversing down from self::

            &gt;&gt;&gt; m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
            &gt;&gt;&gt; d = PyQuery(m)
            &gt;&gt;&gt; d('p').find('em')
            [<em>, <em>]
            &gt;&gt;&gt; d('p').eq(1).find('em')
            [<em>]

        ..
        """
        xpath = selector_to_xpath(selector)
        results = [child.xpath(xpath) for tag in self for child in tag.getchildren()]
        # Flatten the results
        elements = []
        for r in results:
            elements.extend(r)
        return self.__class__(elements, **dict(parent=self))
</em></em></em>
github dsc / pyquery / pyquery / pyquery.py View on Github external
if isinstance(context, basestring):
                try:
                    elements = fromstring(context, self.parser)
                except Exception:
                    raise ValueError(context)
            elif isinstance(context, self.__class__):
                # copy
                elements = context[:]
            elif isinstance(context, list):
                elements = context
            elif isinstance(context, etree._Element):
                elements = [context]

            # select nodes
            if elements and selector is not no_default:
                xpath = selector_to_xpath(selector)
                results = [tag.xpath(xpath) for tag in elements]
                # Flatten the results
                elements = []
                for r in results:
                    elements.extend(r)

        list.__init__(self, elements)
github dsc / pyquery / pyquery / pyquery.py View on Github external
def _filter_only(self, selector, elements, reverse=False, unique=False):
        """Filters the selection set only, as opposed to also including
           descendants.
        """
        if selector is None:
            results = elements
        else:
            xpath = selector_to_xpath(selector, 'self::')
            results = []
            for tag in elements:
                results.extend(tag.xpath(xpath))
        if reverse:
            results.reverse()
        if unique:
            result_list = results
            results = []
            for item in result_list:
                if not item in results:
                    results.append(item)
        return self.__class__(results, **dict(parent=self))