我用这个:
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
print (response.text)
输出结果为:
{"mid":"4432.95","bid":"4432.9","ask":"4433.0","last_price":"4432.9","low":"4276.9","high":"4482.0","volume":"32877.86104158","timestamp":"1506955900.864889"}
我想把最后的价格作为一个浮点数,以便用它进行一些计算......
我无法弄清楚如何做到这一点...... 你能帮我吗 ? THX
答案 0 :(得分:2)
试试这样:
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
data = response.json()
last_price = float(data.get('last_price'))
print(last_price)
答案 1 :(得分:0)
响应是一个JSON,因此如果您在响应中调用requests
方法,.json()
将为您解析它。你只需要这样做:
import requests
resp_json = requests.get("https://api.bitfinex.com/v1/pubticker/btcusd").json()
# resp_json is a native Python dict representing the JSON response content. But since all values are strings in the response, you need to convert them to a float manually.
print(float(resp_json["last_price"]))