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..
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With