Skip to content Skip to sidebar Skip to footer

Python : Lxml Xpath Get Two Different Classes

Here is my sample Python code import requests import lxml.html page = '
2233

Solution 1:

Use the | union operator to join the results of multiple full path expressions:

number = tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')

Demo:

>>> import lxml.html
>>> page = '<divclass="aaaa12"><spanclass="test">22</span><spanclass="number">33</span></div><divclass="dddd13"><span>Kevin</span></div>' 
>>> tree = lxml.html.fromstring(page)
>>> tree.xpath('//span[@class="number"]/text() | //div[@class="dddd13"]/span/text()')
['33', 'Kevin']

Post a Comment for "Python : Lxml Xpath Get Two Different Classes"