We will scrape URL such as https://www.google.com/search?q=bitcoin+price
https://www.google.com/search?q=dogecoin+price
https://www.google.com/search?q=WHAREVER_Coin+price
Here is the Code:-
# Get Crypto Rates in Real Time from bs4 import BeautifulSoup import requests import time def get_crypto_price(coin): url = "https://www.google.com/search?q="+coin+"+price" HTML = requests.get(url) soup = BeautifulSoup(HTML.text, 'html.parser') text = soup.find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).text return text #Choose the cryptocurrency def main(bitcoin): try: last_price = -1 while True: crypto = bitcoin #Get the price of the crypto currency price = get_crypto_price(crypto) #Check if the price changed if price != last_price: #Print the price print(crypto+' price: ',price) #Update the last price last_price = price #Suspend execution for 3 seconds. time.sleep(3) except: print('Please Enter Valid Coin')
Output:-
#NOTE:- type main(<YOUR COIN>) & [ENTER] >>> main('bitcoin') bitcoin price: 43,01,968.93 Indian Rupee >>> main('litecoin') litecoin price: 15,291.76 Indian Rupee >>> main('etherium') etherium price: 3,39,967.56 Indian Rupee >>> main('pythoncoin') Please Enter Valid Coin >>> >>> main('python4fun') Please Enter Valid Coin >>> >>> main('doge') doge price: 16.48 Indian Rupee
RUN
Comments
Post a Comment