ValueError: invalid literal for int() with base 10: '196.41'

uzdisral picture uzdisral · Dec 11, 2017 · Viewed 43.1k times · Source

I don't understand why it works with different scenarios, but not with this one. Basically, some gentleman helped me out HERE with improving my code to scrape weather, which works perfectly. I then tried to do the same to scrape an ETH value which is in a span tag <span class="text-large2" data-currency-value="">$196.01</span>. So, I followed the same technique in the code, replaced the fields, and was hoping for it to work.

The code is here:

import requests
from BeautifulSoup import BeautifulSoup
import time

url = 'https://coinmarketcap.com/currencies/litecoin/'

def ltc():
    while (True):
        response = requests.get(url)
        soup = BeautifulSoup(response.content)
        price_now = int(soup.find("div", {"class": "col-xs-6 col-sm-8 col-md-4 text-left"}).find(
        "span", {"class": "text-large2"}).getText())
        print(u"LTC price is: {}{}".format(price_now))
        # if less than 150
        if 150 > price_now:
            print('Price is Low')
        # if more than 200
        elif 200 < price_now:
            print('Price is high')

if __name__ == "__main__":
    ltc()

The output looks like this:

Traceback (most recent call last):
  File "test2.py", line 24, in <module>
    ltc()
  File "test2.py", line 13, in ltc
    "span", {"class": "text-large2"}).getText())
ValueError: invalid literal for int() with base 10: '196.01'

Then, I finally tried it this way; but from here I get false positives, but no errors. It prints whatever it wants

import requests
from bs4 import BeautifulSoup
import time

url = 'https://coinmarketcap.com/currencies/litecoin/'

def liteCoin():
    while (True):
        response = requests.get(url)
        html = response.text
        soup = BeautifulSoup(html, 'html.parser')
        value = soup.find('span', {'class': 'text-large2'})
        print(''.join(value.stripped_strings))
        if 150 > value:         # if less than 150
            print('Price is Low!')
        elif 200 < value:       # if more than 200
            print('Price is High')
        else:
            print('N/A')
        time.sleep(5)

if __name__ == "__main__":
    liteCoin()

Would the problem be that the value of the ETH has a $ sign inside the span tag? And, that way the program doesn't know what to do with string?

Answer

Robᵩ picture Robᵩ · Dec 11, 2017

First, let's simplify your sample program:

>>> int('196.01')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '196.01'

One cannot convert the string '196.01' to an integer number.

Try this:

>>> int(float('196.01'))
196

Moving from the simple back to the complex, we can do this:

#UNTESTED
price_now = int(float(soup.find("div", {"class": "col-xs-6 col-sm-8 col-md-4 text-left"}).find(
    "span", {"class": "text-large2"}).getText()))