Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find(self, selector):
"""Find elements using selector traversing down from self::
>>> m = '<p><span><em>Whoah!</em></span></p><p><em> there</em></p>'
>>> d = PyQuery(m)
>>> d('p').find('em')
[<em>, <em>]
>>> 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>
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)
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))