Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get text of elements but separate with spaces

I’m grabbing text data from a webpage and when I use .text it has all the elements combined. However, I want to separate some of them with a space.

For example, I have this text:

data=['<span class="sub-title title-block"><span class="nowrap">1.2</span><span class="nowrap">TEKNA</span></span>',
'<span class="sub-title title-block"><span class="nowrap">Amr</span><span class="nowrap">V12 5.2</span></span>',
'<span class="sub-title title-block"></span>']

When I do the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from bs4 import BeautifulSoup
for i in data:
    soup = BeautifulSoup(i, 'lxml')
    for d in soup:
        print(d.text)

I get:

1.2TEKNA
AmrV12 5.2

But I want the expected output:

1.2 TEKNA
Amr V12 5.2

where I get each text separated between each other.

>Solution :

You can use get_text(<sep>) method and define your custom separator as below:

from bs4 import BeautifulSoup

data=['<span class="sub-title title-block"><span class="nowrap">1.2</span><span class="nowrap">TEKNA</span></span>',
'<span class="sub-title title-block"><span class="nowrap">Amr</span><span class="nowrap">V12 5.2</span></span>',
'<span class="sub-title title-block"></span>']

for i in data:
    soup = BeautifulSoup(i, 'lxml')
    for d in soup:
        print(d.get_text(" "))

Output:

1.2 TEKNA
Amr V12 5.2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading