skip to Main Content

For my first experience in Python, I’m using Selenium and Beautifulsoup to retrieve article titles from a website. However, I’m having trouble to take child from a div, I just find the first span but not the second…

I present you the div :

<div class="d-flex justify-content-between font-size-14 font-weight-bold">
                                    <span>Porte</span>
                                    <span>epoxy blanc</span>
</div>

I tried that :

from bs4 import BeautifulSoup

items = soup.find_all("div", {"class": "col-12 col-sm-6 product--description--content--item"})
children = items.find_all("span")
print(len(children))

2

Answers


  1. from bs4 import BeautifulSoup
    
    html = '<div class="d-flex justify-content-between font-size-14 font-weight-bold"> <span>Porte</span> <span>epoxy blanc</span> </div>'
    soup = BeautifulSoup(html, 'html.parser')
    
    spans = soup.find_all('span')
    
    for span in spans:
        print(span.text)
    

    I hope you can modify my code whatever need you.

    Also can try this

    soup = BeautifulSoup(html, 'html.parser')
    
    div = soup.find('div', class_='d-flex justify-content-between font-size-14 font-weight-bold')
    
    spans = div.find_all('span')
    
    for span in spans:
        print(span.text)
    
    
    Login or Signup to reply.
  2. You could use find_next_sibling() to specify the element that you are looking for based on the one you still found:

    soup.span.find_next_sibling('span')
    

    Example:

    from bs4 import BeautifulSoup
    
    html = '<div class="d-flex justify-content-between font-size-14 font-weight-bold"> <span>Porte</span> <span>epoxy blanc</span> </div>'
    soup = BeautifulSoup(html)
    
    soup.span.find_next_sibling('span')
    

    Output:

    <span>epoxy blanc</span>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search