eth_api = "https://api.coinbase.com/v2/exchange-rates?currency=ETH"
headers = {"Accept": "application/json"}
r = requests.request("GET", eth_api, headers=headers)
data2 = r.json()
eth_price = data2['data']['rates']['USD']
print (eth_price)
I am still new to this, tried other ways but they are not effective, i want to reuse this in commands
CodePudding user response:
You can use while loop
and time
. Here's an example for you
import time
import requests
eth_api = "https://api.coinbase.com/v2/exchange-rates?currency=ETH"
headers = {"Accept": "application/json"}
# initialize variable before the loop.
data2 = None
while True:
r = requests.request("GET", eth_api, headers=headers)
data2 = r.json()
eth_price = data2['data']['rates']['USD']
print (eth_price)
# Wait 5 seconds before continuing.
time.sleep(5)
CodePudding user response:
By updating do you mean to run this block of code every 5 seconds. If you are trying to do that then you have to put the block of code in a while loop and put time.sleep(5) to run the loop every 5 seconds. NOTE ~ You can't close the running process or the loop will stop so as your script.