0

I'm trying to make a bot that takes the data of btc prices from google and compares to another site but I can't find a way to make the numbers printed from html turn into a integer(So I can compare the numbers).

the error:

<ipython-input-37-58a648f164e1> in <module>()
     11 tring = text.replace(" United States Dollar", "")
     12 
---> 13 tle = int(tring)
     14 
     15 print(tle)

ValueError: invalid literal for int() with base 10: '43,020.80'

the code:

#make request to website
HTML = requests.get(url)

#parse the html
soup = BeautifulSoup(HTML.text, 'html.parser') 


text = soup.find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).text

tring = text.replace(" United States Dollar", "")

tle = int(tring)

print(tle)```
mangos
  • 1
  • That data doesn't represent an integer but a floating-point number. You need to cast the string to `float`. But first, there are a few ways you can deal with numbers with thousand-separator commas: https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python/6633912 – Simon Crowe Jan 16 '22 at 12:03

1 Answers1

0

If you're only interested in the integer part of '43,020.80' then:

int(float('43,280.50'.replace(',','')))) == 43280
DarkKnight
  • 19,739
  • 3
  • 6
  • 22