Python 谷歌云函数抛出奇怪的错误
这里有人熟悉谷歌云的功能吗?我阅读了他们的文档,在此基础上,我定制了我的脚本,以尝试在他们的托管环境中工作 因此,我的Python脚本如下所示Python 谷歌云函数抛出奇怪的错误,python,python-3.x,google-cloud-platform,google-cloud-functions,Python,Python 3.x,Google Cloud Platform,Google Cloud Functions,这里有人熟悉谷歌云的功能吗?我阅读了他们的文档,在此基础上,我定制了我的脚本,以尝试在他们的托管环境中工作 因此,我的Python脚本如下所示 def main(): requests numpy pandas datetime requests pandas_gbq xml.etree.ElementTree # authentication: working.... login = 'my_email'
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()
requests
numpy
pandas
datetime
requests
pandas_gbq
xml.etree.ElementTree
另外,我的requirements.txt如下所示
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()
requests
numpy
pandas
datetime
requests
pandas_gbq
xml.etree.ElementTree
在过去的两个多月里,我的脚本一直运行良好,但我需要每天在笔记本电脑上运行它。为了摆脱这个手动过程,我正在尝试让它在云上运行。问题是我一直收到一条erorr消息,内容如下:TypeError:main()接受0个位置参数,但给出了1个
在我看来,似乎没有给出任何参数,也没有预期的参数,但不知怎的,谷歌说给出了1个参数。我可以稍微修改代码以使其正常工作,还是以某种方式绕过这个看似良性的错误?谢谢。你误解了云功能的工作原理。它不允许您简单地运行任意脚本。您可以编写触发器来响应HTTP请求,或者在云项目中发生更改时进行响应。你似乎不是在这里干什么。云函数部署不使用main() 您可能需要阅读以了解云函数的用途
如果你试图周期性地运行某个东西,考虑编写一个HTTP触发器,并按照你想要的速率调用一些类似于Cron的服务。
< P>下面的代码使用HTTP触发器将其更改为在谷歌云函数中运行。然后,您可以使用Google Cloud Scheduler按计划调用您的函数。您还需要为需要导入的模块创建一个requirements.txt
。有关更多信息,请参见此
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!!'
def处理程序(请求):
导入请求
进口numpy
进口大熊猫
导入日期时间
导入请求
进口熊猫(gbq)
导入xml.etree.ElementTree
#身份验证:正在工作。。。。
登录名='我的电子邮件'
密码='我的密码'
AsOfDate=datetime.datetime.today().strftime(“%m-%d-%Y”)
#进入URL
请求https://www.business.com/report-api/device=779142&rdate=Yesterday'
response=requests.get(请求\ URL,身份验证=(登录名,密码))
xml_data=response.text.encode('utf-8','ignore')
#tree=etree.parse(xml_数据)
root=xml.etree.ElementTree.fromstring(xml\u数据)
#开始收集数据帧1的根元素和标头
desc=root.get(“说明”)
frm=root.get(“From”)
thru=root.get(“thru”)
loc=根目录获取(“位置”)
loc=loc[:-1]
df1=pandas.DataFrame([['From:',frm],'Through:',Through],'Location:',loc]]
df1.columns=['S','Analytics']
#打印(df1)
#开始获取数据帧2的分析
数据=['Goal:',根[0][0]。文本],'Actual:',根[0][1]。文本],'Compliant:',根[0][2]。文本],'Errors:',根[0][3]。文本],'Checks:',根[0][4]。文本]]
df2=熊猫。数据帧(数据)
df2.columns=['S','Analytics']
#打印(df2)
#将数据帧1与数据帧2合并
df3=df1.append(df2,忽略索引=True)
#打印(df3)
#将说明和今天的日期附加到数据框中
df3['Description']=desc
df3['AsOfDate']=AsOfDate
#从数据框(数据已被转换的地方)推送到Google BQ
pandas_gbq.to_gbq(df3,'度量','分析',chunksize=None,reauth=False,如果存在,则为append',private_key=None,auth_local_webserver=False,table_schema=None,location=None,progress_bar=True,verbose=None)
#打印('执行查询,完成!!')
#通常,对于HTTP触发器,您会在此处返回完整的HTML页面
#你明白了吗
返回“执行查询,完成!!”
是的,是的,你完全正确。我正试图让cron从服务器上运行我的脚本。这就是我想做的。我该怎么做?开始学习HTTP触发器。谢谢我刚刚插入它,收到了以下错误消息:'部署失败:构建失败:{“错误”:{“规范代码”:“参数无效”,“错误消息”:“pip\u下载轮”
had stderr输出:\n找不到满足需求xml.etree.ElementTree的版本(来自-r requirements.txt(第6行))(来自版本:)\nn找不到与xml.etree.ElementTree(来自-r requirements.txt(第6行))匹配的发行版\n\n错误:pip\u下载轮
返回的代码:1”,“errorType:“InternalError”,“errorId:“4B6915C4”}我今天早些时候看到过几次。我没有检查您的导入语句。该错误意味着云函数无法解析包xml
。通过pylint
运行代码,并修复所有警告和错误。这也会再次检查您的导入语句。这就是我所想的。这似乎是一个新的产品,因此,非常不成熟。我肯定能想出一个解决办法。我期待着这件事被建造得比现在多得多。谢谢你在这方面的帮助xml.etree.ElementTree
是一个标准的库模块: