0

I'm making a discord bot that checks the price of DogeCoin, and for that I decided to use BeautifulSoup to check the price (including the currency, which in this case is EUR) on this website: https://www.tradingview.com/symbols/DOGEEUR/?exchange=BINANCE . The price I want to get is immediately below DOGECOIN/EURO [binance symbol] BINANCE

However I'm new to BeautifulSoup and have very basic knowledge and experience with Python so this is quite a big challenge for me.

Is there a way to store the value of the coin + EUR to a string variable?

I tried using the following code as a reference, but even with tutorials I couldn't get any kind of results.

def get_crypto_price(coin):
    #Get the URL
    url = "https://www.google.com/search?q="+coin+"+price"
    
    #Make a request to the website
    HTML = requests.get(url) 
  
    #Parse the HTML
    soup = BeautifulSoup(HTML.text, 'html.parser') 
  
    #Find the current price 
    #text = soup.find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).text
    text = soup.find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).text

#Return the text 
    return text

#Create a main function to consistently show the price of the cryptocurrency
def main():
  #Set the last price to negative one
  last_price = -1
  #Create an infinite loop to continuously show the price
  while True:
    #Choose the cryptocurrency that you want to get the price of (e.g. bitcoin, litecoin)
    crypto = 'bitcoin' 
    #Get the price of the crypto currency
    price = get_crypto_price()
    #Check if the price changed
    if price != last_price:
      last_price = price #Update the last price
      return price
    time.sleep(3) #Suspend execution for 3 seconds.
Mike Malyi
  • 983
  • 8
  • 18
  • The values are dynamically retrieved. You cannot get them from a request to the url you show. Use a dedicated API. – QHarr Feb 11 '21 at 07:39

2 Answers2

2

There appears to be a public API for prices documented here: https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#general-api-information

An example to get a specific item from all (less requests):

import requests

def get_price(symbol, prices):
    for price in prices:
        if symbol == price['symbol']:
            return price['price']
        
prices = requests.get('https://api.binance.com/api/v3/ticker/price').json()
print(get_price('DOGEEUR', prices))

According to the documentation there is a symbol parameter which allows for retrieval of single ticker latest price.


Edit: @Mike Malyi rightly points out [if interested in a single item only) -

Better to add a symbol as a query

https://api.binance.com/api/v3/ticker/price?symbol=DOGETH 

it will save binance weight points, and reduce the size of the request.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Better to add a symbol as a query ```https://api.binance.com/api/v3/ticker/price?symbol=DOGETH``` it will save binance weight points, and reduce the size of the request. – Mike Malyi Feb 11 '21 at 10:33
  • Thanks @MikeMalyi I didn't have time this morning to expand upon my end point about symbol parameter. I have included your really helpful points about weight/size. Thanks again, – QHarr Feb 11 '21 at 17:05
1

You need to pass the crypto name while calling the function.

crypto = 'bitcoin' 
#Get the price of the crypto currency
price = get_crypto_price(crypto)

Please update it is returning the price "32,46,044.24 Indian Rupee"

And because you are comparing it with the number you need to perform operations to get the number from it.

price = float(price.replace(",","").split()[0])

This will return you the price in number : 3255838.53

  • 1
    You're not really answering my question, I understand what you mean and your instructions, but I want the price of Dogecoin from the website I refered on my question, not about the piece of code I put in there – Manuel Carvalho Feb 11 '21 at 04:42