如何使用python连续获取更新的JSON数据

时间:2021-12-07 07:59:36

While working with json in python, I've written this small script to fetch the current price of BTC. This works fine when I run the script in Ubuntu terminal.

在python中使用json时,我写了这个小脚本来获取BTC的当前价格。这在我在Ubuntu终端中运行脚本时工作正常。

What I want is to update the variable current_price for every minute so that I can use this variable to display a desktop notification for current price.

我想要的是每分钟更新变量current_price,以便我可以使用此变量显示当前价格的桌面通知。

I was looking for cron to schedule the task but It'll not work because I want to run the script after each minute, not at a specific time.

我正在寻找cron来安排任务,但它不会工作,因为我想在每分钟后运行脚本,而不是在特定时间。

what is the best practice to achieve this?

实现这一目标的最佳做法是什么?

import urllib, json

url = "https://koinex.in/api/ticker"
response = urllib.urlopen(url)
data = json.loads(response.read())

current_price = data["prices"]["BTC"]

print current_price 

1 个解决方案

#1


0  

To run the script every minute you can use cron job:

要每分钟运行一次脚本,您可以使用cron job:

EX:

* * * * * python /PATH_TO_SCRIPT/script.py

#1


0  

To run the script every minute you can use cron job:

要每分钟运行一次脚本,您可以使用cron job:

EX:

* * * * * python /PATH_TO_SCRIPT/script.py