我正在尝试运行通过搜索分析API拉入数据的云功能。我在Jupyter和Colab笔记本上测试了代码。两者都可以运行。但是,当我在Google cloud中作为云功能部署时,我得到以下错误:
TypeError: main() takes 0 positional arguments but 1 was given
我不相信我在main函数中传递了一个位置参数,但我可能在这里错过了一些东西。下面是我的部分代码:
def main():
def connect(key):
scope = ["https://www.googleapis.com/auth/webmasters.readonly"]
credentials = ServiceAccountCredentials.from_json_keyfile_name(key, scopes=scope)
service = build("searchconsole", "v1", credentials=credentials)
return service
service = connect(key)
def date_range(start_date, end_date, delta=timedelta(days=1)):
current_date = start_date
while current_date <= end_date:
yield current_date
current_date += delta
for date in date_range(start_date, end_date):
date = date.strftime("%Y-%m-%d")
print(date)
i = 0
while True:
request = {
'startDate' : date,
'endDate' : date,
'dimensions' : ["query","page","country","device"],
"searchType": "Web",
'rowLimit' : maxRows,
'startRow' : i * maxRows,
'dimensionFilterGroups': [{
'filters': [{
'dimension': 'country',
'operator': 'equals',
'expression': 'usa'
}]
}]
}
response = service.searchanalytics().query(siteUrl=site_url, body=request).execute()
print()
if response is None:
print("there is no response")
break
if 'rows' not in response:
print("row not in response")
break
else:
for row in response['rows']:
keyword = row['keys'][0]
page = row['keys'][1]
country = row['keys'][2]
device = row['keys'][3]
output_row = [date, keyword, page, country, device, row['clicks'], row['impressions'], row['ctr'], row['position']]
output_rows.append(output_row)
i = i + 1
df = pd.DataFrame(output_rows, columns=['date','query','page', 'country', 'device', 'clicks', 'impressions', 'ctr', 'avg_position'])
csv_file = df.to_csv()
def upload_blob(bucket_name, data, destination_blob_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(data, content_type='text/csv')
upload_blob('gsc-data', csv_file, 'gsc_output.csv')
任何帮助都非常感谢!
Google Cloud Functions入口点必须接受一个参数(一个请求对象),如下所示。
如果你不想使用这个参数,你可以像这样忽略它:
def main(_):
# ...
如果你希望它是可选的,你可以使用:
def main(*args, **kwargs):
# ...