Игорь Проценко Игорь Проценко - 3 months ago 18
Python Question

How to match nested italic font tags with Xpath?

Consider an

xml
structure like the following

<p class="long">
<i>Malicious</i>
" is the adjective based on the noun "
<i>malice</i>
", which means the desire to harm others. Both words come from the latin word "
</p>


I want to select all text inside the
<p>
tag.
I tried with

examples = tree.xpath('//p[@class="long"]/text()')


With this, however, all text betweem tags is ignored for some reason.

What is the correct way to extract all the text inside the
<p>
tags, regardless of it being also contained in other nested tags?

glS glS
Answer

Try with

examples=tree.xpath('//p[@class="long"]//text()')

(with the double slash before text(), which also matches nodes that aren't direct children)