【发布时间】:2019-06-12 04:09:21
【问题描述】:
这里有人熟悉 Google Cloud Functions 吗?我阅读了他们的文档,并在此基础上定制了我的脚本以尝试在他们的托管环境中工作。
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()
另外,我的 requirements.txt 看起来像这样。
requests
numpy
pandas
datetime
requests
pandas_gbq
xml.etree.ElementTree
我的脚本在过去 2 个月内一直运行良好,但我需要每天在笔记本电脑上运行它。为了摆脱这个手动过程,我试图让它在云上运行。问题是我不断收到一条错误消息:TypeError: main() takes 0 positional arguments but 1 was given
对我来说,似乎没有给出任何参数,也没有预期的参数,但不知何故,谷歌说给出了 1 个参数。我可以稍微修改我的代码以使其正常工作,或者以某种方式绕过这个看似良性的错误吗?谢谢。
【问题讨论】:
标签: python python-3.x google-cloud-platform google-cloud-functions