I have the following code to parse html table. How do I check for a specified text in <td> element? This doesn’t work : val=doc.xpath('//tr/td[child::*[text()="Street :"]/span/text()'). I am trying to extract the <span> text only when the <td> text matches ‘Street :’. Any feedback is much appreciated!
import lxml.html as lh
html='''<tr>
<td>
Street : <span> High St. </span>
</td>
</tr>
<tr>
<td>
City : <span> Hightstown </span>
</td>
</tr>'''
doc=lh.fromstring(html)
#val=doc.xpath('//tr/td[child::*[text()="Street :"]/span/text()')
#street=doc.xpath('//tr/td/text()')
val=doc.xpath('//tr/td/span/text()')
#print street
print val
1 Answer