Home > Software design >  Scraping text between two span elements using Beautifulsoup
Scraping text between two span elements using Beautifulsoup

Time:10-28

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 ></span> 
Text which I need to scrape
<span ></span>

CodePudding user response:

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

from bs4 import BeautifulSoup

html_doc = """\
<span ></span> 
Text which I need to scrape
<span ></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

  • Related