我制作了一个机器人,它比较了最后一次购买并通过获取加密货币的交易所销售订单并打印出差异。 我现在的问题是它打印了一遍又一遍收到的最后一个订单,我认为这是因为while循环。有没有办法让它只打印最后两个而不打印相同的东西多次?我在考虑使用OrderedDict,但我不知道如何在Json上使用它。以下是涉及的代码:
import time, requests, json
> while True:
> BU = requests.session()
> URL = 'https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE'
> r = BU.get(URL, timeout=(15, 10))
> time.sleep(1)
> MarketPairs = json.loads(r.content)
> for element in MarketPairs['result']:
> id = element['Id']
> price = element['Price']
> tot = element['Total']
> time = element['TimeStamp']
> type = element['OrderType']
>
>
> if time > '2017-12-11T21:37:01.103':
> print type, id, tot, price, time
> time.sleep(1)
答案 0 :(得分:0)
我想这就是你想要的...... 它将仅打印最后价格,并且仅在与前一个价格不同时才会打印
import requests as req
import time
previous=None
while 1:
url='https://bittrex.com/api/v1.1/public/getmarkethistory?market=BTC-DOGE'
response = req.get(url,timeout=(15,10)).json()
result = response["result"]
last_price_dict = result[0]
id=last_price_dict["Id"]
price = last_price_dict["Price"]
total = last_price_dict["Total"]
timestamp = last_price_dict["TimeStamp"]
order_type=last_price_dict["OrderType"]
this_one = (id, total, price, timestamp)
if id != previous:
print(this_one)
previous = id
time.sleep(3)