Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 636
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 0
Kenil Vasani
Asked: December 14, 20202020-12-14T21:08:07+00:00 2020-12-14T21:08:07+00:00In: Python

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

  • 0

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?

beautifulsoupdata-sciencepythonvalueerrorweb-scraping
  • 1 1 Answer
  • 9 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      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()))
      
      • 5
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains ...

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com