I use tree.xpath
to iterate over all interesting HTML elements but I need to be able to tell whether the current element is part of a certain CSS class or not.
from lxml import html
mypage = """
<div class="otherclass exampleclass">some</div>
<div class="otherclass">things</div>
<div class="exampleclass">are</div>
<div class="otherclass">better</div>
<div>left</div>"""
tree = html.fromstring(mypage)
for item in tree.xpath( "//div" ):
print("testing")
#if "exampleclass" in item.getListOfClasses():
# print("foo")
#else:
# print("bar")
The overall structure should remain the same.
What is a fast way to check whether or not the current div
has the exampleclass
class or not?
In above example, item
is of lxml.html.HtmlElement
class, which has the property classes
but I don't understand what this means:
classes
A set-like wrapper around the 'class' attribute.Get Method:
unreachable.classes(self)
- A set-like wrapper around the 'class' attribute.Set Method:
unreachable.classes(self, classes)
It returns a lxml.html.Classes
object, which has a __iter__
method and it turns out iter()
works. So I construct this code:
for item in tree.xpath( "//div" )
match = False
for classname in iter(item.classes):
if classname == "exampleclass":
match = True
if match:
print("foo")
else:
print("bar")
But I'm hoping there is a more elegant method.
I tried searching for similar questions but all I found were various "how do I get all elements of 'classname'", however I need all div
s in the loop, I just want to treat some of them differently.