谷歌云函数扔怪E

2024-04-19 15:19:00 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有人熟悉谷歌云功能吗?我阅读了他们的文档,并在此基础上定制了我的脚本,试图在他们的托管环境中工作。在

https://cloud.google.com/functions/docs/concepts/python-runtime

所以,我的Python脚本如下所示。在

def main():

    requests
    numpy
    pandas
    datetime
    requests
    pandas_gbq
    xml.etree.ElementTree


    # authentication: working....
    login = 'my_email' 
    password = 'my_password'


    AsOfDate = datetime.datetime.today().strftime('%m-%d-%Y')

    #step into URL
    REQUEST_URL = 'https://www.business.com/report-api/device=779142&rdate=Yesterday'
    response = requests.get(REQUEST_URL, auth=(login, password))
    xml_data = response.text.encode('utf-8', 'ignore') 

    #tree = etree.parse(xml_data)
    root = xml.etree.ElementTree.fromstring(xml_data)

    # start collecting root elements and headers for data frame 1
    desc = root.get("Description")
    frm = root.get("From")
    thru = root.get("Thru")
    loc = root.get("locations")
    loc = loc[:-1]
    df1 = pandas.DataFrame([['From:',frm],['Through:',thru],['Location:',loc]])
    df1.columns = ['S','Analytics']
    #print(df1)

    # start getting the analytics for data frame 2
    data=[['Goal:',root[0][0].text],['Actual:',root[0][1].text],['Compliant:',root[0][2].text],['Errors:',root[0][3].text],['Checks:',root[0][4].text]]
    df2 = pandas.DataFrame(data)
    df2.columns = ['S','Analytics']
    #print(df2)

    # merge data frame 1 with data frame 2
    df3 = df1.append(df2, ignore_index=True)
    #print(df3)

    # append description and today's date onto data frame
    df3['Description'] = desc
    df3['AsOfDate'] = AsOfDate


    # push from data frame, where data has been transformed, into Google BQ
    pandas_gbq.to_gbq(df3, 'Metrics', 'analytics', chunksize=None, reauth=False, if_exists='append', private_key=None, auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, verbose=None)
    print('Execute Query, Done!!')

main()

if __name__ == '__main__':
    main()  

还有,我的要求.txt看起来像这样。在

^{pr2}$

在过去的2个多月里,我的脚本运行得很好,但我需要每天在我的笔记本电脑上运行它。为了摆脱这个手动过程,我试图让它在云上运行。问题是我一直收到一条erorr消息,内容是:TypeError: main() takes 0 positional arguments but 1 was given

在我看来,似乎没有给出任何论据,也没有预期的论据,但不知何故,谷歌说给出了1个论据。我能稍微修改一下我的代码使其正常工作吗,或者以某种方式绕过这个看似良性的错误?谢谢。在


Tags: text脚本nonepandasdatagetmainroot
2条回答

下面将使用HTTP触发器将代码更改为在Google云函数中运行。然后可以使用googlecloudscheduler按计划调用函数。您还需要使用需要导入的模块创建一个requirements.txt。有关详细信息,请参阅此document。在

def handler(request):

    import requests
    import numpy
    import pandas
    import datetime
    import requests
    import pandas_gbq
    import xml.etree.ElementTree


    # authentication: working....
    login = 'my_email' 
    password = 'my_password'


    AsOfDate = datetime.datetime.today().strftime('%m-%d-%Y')

    #step into URL
    REQUEST_URL = 'https://www.business.com/report-api/device=779142&rdate=Yesterday'
    response = requests.get(REQUEST_URL, auth=(login, password))
    xml_data = response.text.encode('utf-8', 'ignore') 

    #tree = etree.parse(xml_data)
    root = xml.etree.ElementTree.fromstring(xml_data)

    # start collecting root elements and headers for data frame 1
    desc = root.get("Description")
    frm = root.get("From")
    thru = root.get("Thru")
    loc = root.get("locations")
    loc = loc[:-1]
    df1 = pandas.DataFrame([['From:',frm],['Through:',thru],['Location:',loc]])
    df1.columns = ['S','Analytics']
    #print(df1)

    # start getting the analytics for data frame 2
    data=[['Goal:',root[0][0].text],['Actual:',root[0][1].text],['Compliant:',root[0][2].text],['Errors:',root[0][3].text],['Checks:',root[0][4].text]]
    df2 = pandas.DataFrame(data)
    df2.columns = ['S','Analytics']
    #print(df2)

    # merge data frame 1 with data frame 2
    df3 = df1.append(df2, ignore_index=True)
    #print(df3)

    # append description and today's date onto data frame
    df3['Description'] = desc
    df3['AsOfDate'] = AsOfDate


    # push from data frame, where data has been transformed, into Google BQ
    pandas_gbq.to_gbq(df3, 'Metrics', 'analytics', chunksize=None, reauth=False, if_exists='append', private_key=None, auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, verbose=None)
    # print('Execute Query, Done!!')

    # Normally for an HTTP trigger you would return a full HTML page here
    # <html><head></head><body>you get the idea</body></html>
    return 'Execute Query, Done!!'

你误解了云函数的工作原理。它不允许您简单地运行任意脚本。您可以编写触发器来响应HTTP请求,或者当您的云项目发生变化时。你在这里不是为了这个。云函数部署不使用main()。在

您可能需要阅读overview documentation,以了解云函数的用途。在

如果您尝试定期运行某个东西,请考虑编写一个HTTP触发器,并让某个类似cron的服务以您想要的速率调用它。在

相关问题 更多 >