Frage

Can I use element.findall() from xml.etree.ElementTree to find an element with multiple values in a single attribute?

I can use this to find all of the div elements with class="day":

from xml.etree.ElementTree import ElementTree

calendar = ElementTree()
calendar.parse("calendar-week.xml")

calendar.findall('.//div[@class="day"]')

or this to find all of the div elements with the class="first day":

calendar.findall('.//div[@class="first day"]')

but is there way to find all of the div elements with "day" in their class? I can not find any docs on how to do this. Is there a way to use regular expressions here?

<body>
   <div class="calendar-week">
      <div class="first day" id="su">Sunday</div>
      <div class="day" id="mo">Monday</div>
      <div class="day" id="tu">Tuesday</div>
      <div class="day" id="we">Wednesday</div>
      <div class="day" id="th">Thursday</div>
      <div class="day" id="fr">Friday</div>
      <div class="last day" id="sa">Saturday</div>
      <div class="clear-float"></div>
   </div><!-- /calendar-week -->
</body>    
War es hilfreich?

Lösung 2

This code worked based on the xpath term provided in the answer from @stranac

from lxml import etree as ET

calendar = ET.ElementTree()
calendar.parse("calendar-week.xml")

elem_list = calendar.xpath('//div[contains(concat(" ", @class, " "), " day ")]')

Andere Tipps

This xpath is how you would usually do this:

'//div[contains(concat(" ", @class, " "), " day ")]'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top