extract text without tags with beautifulsoup

Viewed 107

I want to extract this text

2020; 18 (1) DOI

I can't do it, can anyone help me?

My webpage looks like this:

<div class="col-md-10"><span class="title"><a href="/article/00731b64b7ae44bb96e5cd51edaa113d">Medical Device-Related Pressure Injury in health care professionals in times of pandemic</a></span><br><em>Aline Oliveira Ramalho, Paula de Souza Silva Freitas, Paula Cristina Nogueira</em><br><a href="/toc/2595-7007">Estima</a>. 2020;18(1) DOI <a href="https://doi.org/10.30886/estima.v18.867_IN">10.30886/estima.v18.867_IN</a><br><a class="doaj-public-search-abstractaction doaj-public-search-abstractaction-results" href="#" rel="00731b64b7ae44bb96e5cd51edaa113d"><strong>Abstract</strong></a> | <a href="https://www.revistaestima.com.br/index.php/estima/article/view/867/pdf">Full Text</a><div class="doaj-public-search-abstracttext doaj-public-search-abstracttext-results" rel="00731b64b7ae44bb96e5cd51edaa113d" style="display:none">Facing the number of cases of coronavirus infection (COVID-19).</div></div>

Thank you

2 Answers

You can use re and next_sibling.You can try it:

from bs4 import BeautifulSoup
import re
html_doc = """<div class="col-md-10"><span class="title"><a href="/article/00731b64b7ae44bb96e5cd51edaa113d">Medical Device-Related Pressure Injury in health care professionals in times of pandemic</a></span><br><em>Aline Oliveira Ramalho, Paula de Souza Silva Freitas, Paula Cristina Nogueira</em><br><a href="/toc/2595-7007">Estima</a>. 2020;18(1) DOI <a href="https://doi.org/10.30886/estima.v18.867_IN">10.30886/estima.v18.867_IN</a><br><a class="doaj-public-search-abstractaction doaj-public-search-abstractaction-results" href="#" rel="00731b64b7ae44bb96e5cd51edaa113d"><strong>Abstract</strong></a> | <a href="https://www.revistaestima.com.br/index.php/estima/article/view/867/pdf">Full Text</a><div class="doaj-public-search-abstracttext doaj-public-search-abstracttext-results" rel="00731b64b7ae44bb96e5cd51edaa113d" style="display:none">Facing the number of cases of coronavirus infection (COVID-19).</div></div>"""

soup = BeautifulSoup(html_doc, 'lxml')

div = soup.find("div")

result = div.find("a", attrs={"href": re.compile("^/toc/2595-7007.*")}).next_sibling

result = result.replace('.',"")

print(result)

Output will be:

2020;18(1) DOI

If you have BS 4.7.1 or above you can use below css selector.

html='''<div class="col-md-10"><span class="title"><a href="/article/00731b64b7ae44bb96e5cd51edaa113d">Medical Device-Related Pressure Injury in health care professionals in times of pandemic</a></span><br><em>Aline Oliveira Ramalho, Paula de Souza Silva Freitas, Paula Cristina Nogueira</em><br><a href="/toc/2595-7007">Estima</a> 2020;18(1) DOI <a href="https://doi.org/10.30886/estima.v18.867_IN">10.30886/estima.v18.867_IN</a><br><a class="doaj-public-search-abstractaction doaj-public-search-abstractaction-results" href="#" rel="00731b64b7ae44bb96e5cd51edaa113d"><strong>Abstract</strong></a> | <a href="https://www.revistaestima.com.br/index.php/estima/article/view/867/pdf">Full Text</a><div class="doaj-public-search-abstracttext doaj-public-search-abstracttext-results" rel="00731b64b7ae44bb96e5cd51edaa113d" style="display:none">Facing the number of cases of coronavirus infection (COVID-19).</div></div>'''
soup=BeautifulSoup(html,"html.parser")
print(soup.select_one('a:contains("Estima")').next_sibling)
Related