Last active
May 22, 2020 05:04
-
-
Save mattvukas/9146611 to your computer and use it in GitHub Desktop.
A simple Python script that queries the Bitstamp API every 5 seconds and prints the last USD/Bitcoin price to the shell. Requires the Python Requests library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /usr/bin/python3 | |
import requests | |
import json | |
from time import sleep | |
def getBitcoinPrice(): | |
URL = "https://www.bitstamp.net/api/ticker/" | |
try: | |
r = requests.get(URL) | |
priceFloat = float(json.loads(r.text)["last"]) | |
return priceFloat | |
except requests.ConnectionError: | |
print("Error querying Bitstamp API") | |
while True: | |
print("Bitstamp last price: $" + str(getBitcoinPrice()) + "/BTC") | |
sleep(5) |
I made a similar script myself. Both yours and mine fail to obtain data from the website. Any clue why? (That happens when I run code on py desktop from a file but it retrieves it if I run it in debugger or on repl.it.)
@namedTim
thats interesting. Can u open the url in the Browser? Is there an error code? do u installed the module? is script correct formatted?
@Smugmug24 I found the error. Orange/anaconda was messing with the path and so my script was calling python but not the real one (i don't know how to explain this) but Atom editor was detecting the right one.
SOLUTION: I just refreshed the default way you open .pyw and .py files in Windows.
@namedTim
nice :)
Python 3 Version
#/usr/bin/python3
import requests, json
from time import sleep
def getBitcoinPrice():
URL = 'https://www.bitstamp.net/api/ticker/'
try:
r = requests.get(URL)
priceFloat = float(json.loads(r.text)['last'])
return priceFloat
except requests.ConnectionError:
print("Error querying Bitstamp API")
while True:
print("Bitstamp last price: $" + str(getBitcoinPrice()) + "/BTC")
sleep(5)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi,
Your code worked well, I tried to convert it to binance.us but it did not work, can you please help change it to binance.us?
thanks in advance.