Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Lxml find text after <strong></strong> tags

Tags:

python

lxml

Please can you find a solution to this simple problem.

<strong>text1</strong>: text2 

I am trying to scrape this html part, So I need to get text1 and text2 separately. How to do that? It should be something like :

x = tree.xpath('//strong[text()="text1"]/text()') 

But this code returns actual "text1" , and I need text2 also..

like image 811
Nikanor Avatar asked Aug 04 '26 18:08

Nikanor


1 Answers

You need to get the strong tag element, and then use element.tail to get the text after it. Example -

In [12]: from lxml import html

In [13]: tree = html.fromstring("<strong>text1</strong>: text2 ")

In [14]: x = tree.xpath('//strong[text()="text1"]')

In [15]: for i in x:
   ....:     print(i.tail)
   ....:
: text2

This would also work for lxml.etree , not just lxml.html . Example -

In [16]: from lxml import etree

In [18]: tree = etree.fromstring("<elem><strong>text1</strong>: text2</elem>")

In [19]: x = tree.xpath('//strong[text()="text1"]')

In [20]: for i in x:
   ....:     print(i.tail)
   ....:
: text2

To get both of them together , you can do -

In [21]: x = tree.xpath('//strong[text()="text1"]')

In [23]: for i in x:
   ....:     print('text :',i.text)
   ....:     print('tail :',i.tail)
   ....:
text : text1
tail : : text2
like image 123
Anand S Kumar Avatar answered Aug 07 '26 09:08

Anand S Kumar