'How to get text from a div span in soup?

Hi I am trying to get the text within a span from beautiful soup however it doesn't return the 631. I want to get the 631 from this html.

<div class="jsx-3024393758 jsx-3850983025 sold">
  <img alt="sold" src="data:imagesd">
  <span class="jsx-3024393758 jsx-3850983025">
    <span class="jsx-3024393758 jsx-3850983025 sold-text">Sold</span> "631"</span>
</div>

My code right now is like this.

sold = soup.select_one('jsx-3024393758 jsx-3850983025 sold-text','.jsx-3024393758 jsx-3850983025 sold').get('style', '').replace('width:','').text

However it returns null Please do help, and if a little explanation of where I went wrong would be very helpful.



Solution 1:[1]

There are multiple issues with your code and it seems to be a wild mix of things that do not focus your issue. So concerning your question take a minute or two to read css selectors to get an idea of chaining selectors.


In this specific case I would select the <div> and extract pick the last element from stripped_strings:

list(soup.select_one('div.sold').stripped_strings)[-1]
Example
html = '''
<div class="jsx-3024393758 jsx-3850983025 sold">
  <img alt="sold" src="data:imagesd">
  <span class="jsx-3024393758 jsx-3850983025">
    <span class="jsx-3024393758 jsx-3850983025 sold-text">Sold</span> "631"</span>
</div>
'''
soup = BeautifulSoup(html)

list(soup.select_one('div.sold').stripped_strings)[-1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1