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?
ValueError: invalid literal for int() with base 10 occurs when you convert the string or decimal or characters values not formatted as an integer. To solve the error, you can use the float() method to convert entered decimal input and then use the int() method to convert your number to an integer.
The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. To solve this problem, you can use the float() method to convert a floating-point number in a string to an integer.
invalid literal for int() with base 10. The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.
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()))
You need to understand types in Python you are getting a float not an int and you need to cast the float to a string to print it. So there are two changes needed.
price_now = float(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(str(price_now)))
Outputs:
LTC price is: 195.44
LTC price is: 195.44
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With