Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text after the tag

Tags:

python

xpath

lxml

How to get the text after the tag is found

Example:

#!/usr/bin/env python
import lxml.html

html = """
<b>Point1:</b> Text1 <br>
<b>Point2:</b> Text2 <br>
...
<b>PointN:</b> TextN
<b>PointN+1:</b> TextN+1<br>
"""
dom = lxml.html.document_fromstring(html)
el = dom.xpath('//b[text()="PointN:"]')
print el

tag el with the text PointN found out how to get text TextN?

like image 857
alexandris Avatar asked Dec 31 '25 11:12

alexandris


2 Answers

Since TextN follows the <b> that you already found, you can use the XPath following axis:

dom.xpath('//b[text() = "PointN:"]/following::node()')[0]
like image 145
Fred Foo Avatar answered Jan 02 '26 23:01

Fred Foo


Another way is:

el = dom.xpath('//b[text()="PointN:"]')[0]
print el.tail
like image 23
unutbu Avatar answered Jan 03 '26 00:01

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!