Norman Weng
Norman Weng

Reputation: 25

How to find text in specific tag wih lxml and python?

Assuming html source are as follows:

some other content here
<div class="box">
    <h5>this is another one title</h5>
    <p>text paragraph 1 here</p>
    <p>text paragraph 2 here</p>
    <p>text paragraph n here</p>
</div>
<div class="box">
    <h5>specific title</h5>
    <p>text paragraph 1 here</p>
    <p>text paragraph 2 here</p>
    <p>text paragraph 3 here</p>
    <p>text paragraph 4 here</p>
    <small>some specific character:here are some character</small>
</div>
<div class="box">
    <h5>this is another tow title</h5>
    <p>text paragraph 1 here</p>
    <p>text paragraph 2 here</p>
     <p>text paragraph n here</p>
</div>
some other content here

if I want the output are:

specific title

text paragraph 1 here
text paragraph 2 here
text paragraph 3 here
text paragraph 4 here

I want to get specific title and paragraph text. I want to use lxml with python!! Please help me, What should I do?

Upvotes: 1

Views: 610

Answers (1)

falsetru
falsetru

Reputation: 368894

Using xpath expression .//h5[text()="specific title"]/following-sibling::p/text() which will select p tag texts next to h5 tag with specific title:

>>> import lxml.html
>>>
>>> s = '''
... <html>
... some other content here
    ...
... <div class="box">
... <h5>specific title</h5>
... <p>text paragraph 1 here</p>
... <p>text paragraph 2 here</p>
... <p>text paragraph 3 here</p>
... <p>text paragraph 4 here</p>
... <small>some specific character:here are some character</small>
... </div>
... <div class="box">
... <h5>this is another tow title</h5>
    ...
... </div>
... some other content here
... </html>
... '''
>>>
>>> root = lxml.html.fromstring(s)
>>> root.xpath('.//h5[text()="specific title"]/following-sibling::p/text()')
['text paragraph 1 here', 'text paragraph 2 here', 'text paragraph 3 here',
 'text paragraph 4 here']
>>> print('\n'.join(root.xpath(
        './/h5[text()="specific title"]/following-sibling::p/text()')))
text paragraph 1 here
text paragraph 2 here
text paragraph 3 here
text paragraph 4 here

Upvotes: 2

Related Questions