代码展现:
import time
import requests
while True:
time.sleep(1)
date = time.time()
time_local = time.localtime(date)
date_time = time.strftime('%Y-%m-%d %H:%M:%S',time_local)
def convert_currrency(amount,from_currency,to_currency):
url = f'https://api.exchangerate-api.com/v4/latest/{from_currency}'
response = requests.get(url)
data = response.json()
exchange_rate = data['rates'][to_currency]
converted_amount = amount*exchange_rate
return converted_amount
if __name__ == '__main__':
amount=1
from_currency = 'USD'
to_currency = 'CNY'
convert_amount = convert_currrency(amount,from_currency,to_currency)
print(f'{date_time}:')
print(f"{amount}{from_currency}兑换成{to_currency}:{convert_amount}")
结果展现:
说明:我获取的是每秒钟的数据,可以根据自己的需求,设置时间间隔。最难的其实是找到这个接口。这是外网的网站,需要一点魔法手段。