Mohamed Hedeya
Mohamed Hedeya

Reputation: 171

Scraping text between two span elements using Beautifulsoup

I was scraping a site using Beautifulsoup, and found text between 2 span elements (not between opening and closing tags).

How can I scrap the text between two span elements, such as:

<span class="description_start"></span> 
Text which I need to scrape
<span class="description_end"></span>

Upvotes: 1

Views: 383

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195593

Try to find first <span> and then .find_next(text=True):

from bs4 import BeautifulSoup

html_doc = """\
<span class="description_start"></span> 
Text which I need to scrape
<span class="description_end"></span>"""

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

t = soup.find("span", class_="description_start").find_next(text=True)
print(t)

Prints:

 
Text which I need to scrape

Upvotes: 1

Related Questions