0

I'd like to know how to get $39,465,077,974.88 from this beautifulsoup code

<td><span>$39,465,077,974.88</span><div><span class="sc-15yy2pl-0 kAXKAX" style="font-size:12px;font-weight:600"><span class="icon-Caret-up"></span>4.59<!-- -->%</span></div></td>

I'm new to web scrapers , hopefully you guys have a clear explanation.

Dev Nocz
  • 35
  • 3

2 Answers2

2

try using a css selector,

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, "html.parser")

print(soup.select_one("td > span").text)

$39,465,077,974.88
sushanth
  • 8,275
  • 3
  • 17
  • 28
1

You can get the value like this. Since the value you need is inside a <span> that is inside a <td>

  • First select the <td> tag using find()

    td = soup.find('td')
    
  • Next select the <span> tag present inside td.

    sp = td.find('span')
    
  • Print the text of sp

    print(sp.text.strip())
    

Here is the complete code

from bs4 import BeautifulSoup

s = """<td><span>$39,465,077,974.88</span><div><span class="sc-15yy2pl-0 kAXKAX" style="font-size:12px;font-weight:600"><span class="icon-Caret-up"></span>4.59<!-- -->%</span></div></td>"""

soup = BeautifulSoup(s, 'lxml')
td = soup.find('td')
sp = td.find('span')
print(sp.text.strip())
$39,465,077,974.88
Ram
  • 4,724
  • 2
  • 14
  • 22