First we create a theme document to test out: >>> from lxml.html import fromstring, tostring >>> theme = fromstring('''\ ... ... ... This is a theme title ... ... ... ... ... ... ... ...
... This content will be replaced. ...
... ... ... ... ... ''', ... base_url='http://somesite.com/theme/theme.html') Then, lets select something: >>> from deliverance.selector import Selector >>> def t_select(selection): ... selector = Selector.parse(selection) ... type, elements, attributes = selector(theme) ... if type == 'attributes': ... for element in elements: ... if not attributes: ... attributes = element.attrib.keys() ... text = [] ... for key in sorted(attributes): ... text.append('%s="%s"' % (key, element.attrib[key])) ... print 'attributes:%s %s' % (element.tag, ' '.join(text)) ... return ... if type == 'tag': ... for element in elements: ... tag = tostring(element).split('>')[0] + '>' ... print 'tag:%s' % tag ... return ... if type == 'elements': ... type = '' ... else: ... type += ':' ... for element in elements: ... print '%s%s' % (type, tostring(element).strip()) >>> t_select('link') >>> t_select('/html/head/title') This is a theme title >>> t_select('children:title') children:This is a theme title >>> t_select('attributes(class):#header') attributes:div class="title-bar" >>> t_select('#nothing') >>> t_select('div')
This content will be replaced.
>>> t_select('div#header') >>> t_select("tag://div[@id='header']") tag: