search

Home  >  Q&A  >  body text

Extract text from multiple span elements without classes - using BeautifulSoup

This is what the HTML looks like:

<p class="details">
<span>detail1</span>
<span class="number">1</span>
<span>detail2</span>
<span>detail3</span>
</p>

I need to extract detail2 and detail3.

But using this code, I can only get detail1.

info = data.find("p", class_ = "details").span.text

How do I extract the required items?

Thanks in advance!

P粉903052556P粉903052556443 days ago951

reply all(2)I'll reply

  • P粉041856955

    P粉0418569552023-09-16 15:38:11

    In your case, select a more specific element, i.e. select all sibling elements of a element with class number:

    soup.select('span.number ~ span')

    Example

    from bs4 import BeautifulSoup
    html='''<p class="details">
    <span>detail1</span>
    <span class="number">1</span>
    <span>detail2</span>
    <span>detail3</span>
    </p>'''
    soup = BeautifulSoup(html)
    
    [t.text for t in soup.select('span.number ~ span')]

    Output

    ['detail2', 'detail3']

    reply
    0
  • P粉099145710

    P粉0991457102023-09-16 14:52:27

    You can find all <span> and do a normal index:

    from bs4 import BeautifulSoup
    
    html_doc = """\
    <p class="details">
    <span>detail1</span>
    <span class="number">1</span>
    <span>detail2</span>
    <span>detail3</span>
    </p>"""
    
    soup = BeautifulSoup(html_doc, "html.parser")
    
    spans = soup.find("p", class_="details").find_all("span")
    
    for s in spans[-2:]:
        print(s.text)
    

    Output result:

    detail2
    detail3
    

    Or use CSS selector:

    spans = soup.select(".details span:nth-last-of-type(-n+2)")
    
    for s in spans:
        print(s.text)
    

    Output result:

    detail2
    detail3
    

    reply
    0
  • Cancelreply