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

beautifulsoup, how to get text ignoring elements

it is possible to filter out only the text from the following structure:

<font>
   <em>X</em>
   and
   <em>Y</em>
</font>

to obtain the following output:

output = "X and Y"

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

>Solution :

Try:

from bs4 import BeautifulSoup

html_doc = """\
<font>
   <em>X</em>
   and
   <em>Y</em>
</font>"""

soup = BeautifulSoup(html_doc, "html.parser")

out = soup.find("font").get_text(strip=True, separator=" ")
print(out)

Prints:

X and Y
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