Solution 1 :

Try this

from io import StringIO
from bs4 import BeautifulSoup as bs

data = """<div class="C(#959595) Fz(11px) D(ib) Mb(6px)">
    <span>VALUE 1</span>
    <i aria-hidden="true" class="Mx(4px)">•</i>
    <span>TRYING TO GET THIS</span>
</div>
<div class="another class">
    <span>VALUE 1</span>
    <i aria-hidden="true" class="Mx(4px)">•</i>
    <span>TRYING TO GET THIS</span>
</div>"""

soup = bs(StringIO(data))
spans = soup.select('div[class="C(#959595) Fz(11px) D(ib) Mb(6px)"] > span')
print(spans[1].text)

Solution 2 :

You basically had it, you just need to get at the second span in each the div (find_next):

soup = BeautifulSoup(HTML, 'html.parser')
divs = soup.find_all('div', {'class': 'C(#959595) Fz(11px) D(ib) Mb(6px)'})
for div in divs:
    # want the second span in the div
    span = div.find_next('span').find_next('span')
    print(span.string)

Solution 3 :

There are several ways to get the value you want.

from simplified_scrapy.simplified_doc import SimplifiedDoc
html='''
<div class="C(#959595) Fz(11px) D(ib) Mb(6px)">
    <span>VALUE 1</span>
    <i aria-hidden="true" class="Mx(4px)">•</i>
    <span>TRYING TO GET THIS</span>
</div>
'''
doc = SimplifiedDoc(html)
divs = doc.getElementsByClass('C(#959595) Fz(11px) D(ib) Mb(6px)')
for div in divs:
  value = div.getElementByTag('span',start='</span>') # Use start to skip the first
  print (value)
  value = div.getElementByTag('span',before='<span>',end=len(div.html)) # Locate the last
  print (value)
  value = div.i.next # Use <i> to locate
  print (value)
  value = div.spans[-1]
  print (value)
  print (value.text)

Result:

{'tag': 'span', 'html': 'TRYING TO GET THIS'}
{'tag': 'span', 'html': 'TRYING TO GET THIS'}
{'tag': 'span', 'html': 'TRYING TO GET THIS'}
{'tag': 'span', 'html': 'TRYING TO GET THIS'}
TRYING TO GET THIS

Solution 4 :

div= soup.find_all('div',class_='C(#959595) Fz(11px) D(ib) Mb(6px)')
[x.get_text() for x in div[0].find_all('span')]

#op

Out[17]:
['VALUE 1', 'TRYING TO GET THIS']

Problem :

I’m trying to get the second span value in this div and others like it (shown below)

<div class="C(#959595) Fz(11px) D(ib) Mb(6px)">
    <span>VALUE 1</span>
    <i aria-hidden="true" class="Mx(4px)">•</i>
    <span>TRYING TO GET THIS</span>
</div>

I’ve tried looking at similar stack posts, but I still couldn’t figure out how to fix this.
Here’s my current code:

time = soup.find_all('div', {'class': 'C(#959595) Fz(11px) D(ib) Mb(6px)'})
    for i in time:
        print(i.text) #this prints VALUE 1 x amount of times (there are multiple divs)

I’ve tried things like i.span, i.contents, i.children, etc.
I’d really appreciate any help, thanks!

Comments

Comment posted by AMC

Have you consulted the BeatifulSoup docs?

Comment posted by Eli Front

Is there something cleaner like “doc.find_last(‘span’)”

Comment posted by spacecowboy

span = div.find_all(‘span’).pop()

By