python - Finding attribute value using lxml without using a for loop -
this code have @ moment:
>>>p = [] >>>r = root.findall('.//*div/[@class="countdown closed"]/') >>>r '<abbr data-utime="1383624000" class="timestamp"/>' >>>for in r: s = i.attrib p.append(s['data-utime']) >>>p ['1383624000']
s yields:
{'class': 'timestamp', 'data-utime': '1383624000'}
i think code above verbose(creating list, using loop 1 string).
i know lxml capable of achieving more succinctly unable achieve this, appreciate assistance.
use xpath, not elementtree findall()
(which more limited , restricted language present compatibility elementtree library lxml extends), , address path way down attribute:
root.xpath('//html:div[@class="countdown closed"]/@data-utime', namespaces={'html': 'http://www.w3.org/1999/xhtml'})
(it is possible use namespace wildcards in xpath, not great practice -- not leave 1 open namespace collisions, can performance impediment if engine indexes against fully-qualified attribute names).
Comments
Post a Comment