获取每小时变化的数据

问题描述 投票:0回答:1

我正在尝试检索网页 XE.com 的信息,以便在一个天蓝色函数中将欧元转换为哥伦比亚比索,该函数必须在今天、明天下午和晚上运行 3 次,我成功地用 Pandas 做到了这一点,但这仅在第一次有效,因为该值保留在解释器内存中并且每个记录显示相同的更改,我无法重新启动它或恢复它,任何人都知道我该怎么做,?

pandas 实现:-

import pandas as pd

url_EUR_COP = 'https://www.xe.com/currencyconverter/convert/?Amount=1&From=EUR&To=COP'
lst_url_EUR_COP = pd.read_html(url_EUR_COP)
df_trm_EUR_COP = pd.DataFrame(lst_url_EUR_COP[0])

其他选项:

exit()
quit() 

上面的附加选项不起作用并在函数末尾生成错误

在这种情况下,我的文件中的 6 分钟之间存在不小的差异;但页面上有。

enter image description here

python azure-functions interpreter
1个回答
0
投票

这是 pandas 从网页获取数据时的缓存问题。您可能会收到相同的值,因为数据正在保存在内存中并重新用于连续调用,这可能会阻止其更新。

在您的 Function init.py 中使用如下请求库:-

My init.py

import pandas as pd
import requests
import azure.functions as func
import json

def fetch_eur_to_cop_conversion():
    url_EUR_COP = 'https://www.xe.com/currencyconverter/convert/?Amount=1&From=EUR&To=COP'
    response = requests.get(url_EUR_COP)
    lst_url_EUR_COP = pd.read_html(response.text)
    df_trm_EUR_COP = pd.DataFrame(lst_url_EUR_COP[0])
    return df_trm_EUR_COP.to_dict()

def main(req: func.HttpRequest) -> func.HttpResponse:
    conversion_data = fetch_eur_to_cop_conversion()
    response = {
        "conversion_data": conversion_data
    }
    return func.HttpResponse(
        json.dumps(response),
        status_code=200,
        mimetype="application/json"
    )

输出:- 本地:-

enter image description here

enter image description here

我在 Azure Function 应用程序中部署了相同的代码并获得了成功的结果:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.