Reputation: 5742
With a general XPath (or with specific functions of lxml in python), how do you select a set of elements that have a set of tags like this?
<div class="cl1 a">
<div class="cl1 b">
but not
<div class="cl1">
Upvotes: 0
Views: 240
Reputation: 880259
You could use the XPath //div[starts-with(@class,"cl1 ")]
; note the space after cl1
. For example,
In [20]: import lxml.html as LH
In [21]: doc = LH.parse('data.html')
In [24]: doc.xpath('//div[starts-with(@class,"cl1 ")]')
Out[24]: [<Element div at 0x7f0568c68100>, <Element div at 0x7f0568c68158>]
In [25]: [LH.tostring(elt) for elt in doc.xpath('//div[starts-with(@class,"cl1 ")]')]
Out[25]: ['<div class="cl1 a"></div>\n', '<div class="cl1 b"></div>\n']
Upvotes: 3