我有以下代码
client = bigquery.Client()
dataset_id = 'dataset' # replace with your dataset ID
table_id = 'table' # replace with your table ID
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref) # API request
rows_to_insert = []
bq = bigquery.Client(project='project-id')
query = """SELECT Url FROM `project-id.dataset.urltable`"""
query_job = bq.query(query)
data = query_job.result()
rows = list(data)
def main():
for row in rows:
URL = urllib.request.urlopen(row[0])
soup_page = soup(URL, features="lxml")
try:
data = json.loads(soup_page.find_all('script', type='application/ld+json')[1].text)
except:
data ='unknown'
try:
price_ruw = data['offers']['price']
shopprice = price_ruw.replace(',','.')
except:
price = 0
try:
ean = data['gtin13']
ean = str(ean)
except:
ean = 'unknown'
try:
title_ruw1 = data['name']
title_ruw = title_ruw1
tile_trim = title_ruw[:750]
title = tile_trim.replace("'", "")
except:
title = "unknown"
try:
reviews = data['aggregateRating']['reviewCount']
except:
reviews = 0
try:
score = (float(data['aggregateRating']['ratingValue']) * 2)
except:
score = 0
datenow = (datetime.datetime.now())
shoplink = row[0]
rows_to_insert.append([shoplink,ean,title,reviews,score,shopprice,datenow])
client.insert_rows(table, rows_to_insert) # API request
main()
在 Google Cloud 平台中测试此代码给出
Error: function crashed. Details:
main() takes 0 positional arguments but 2 were given
但是,在部署此代码时,它不会出错。仅安排此查询不起作用,因为它不断给出以下错误。
对于部署,我使用以下命令(有效)
gcloud functions deploy <function> --entry-point main --
runtime python37 --trigger-resource <name> --trigger-event google.pubsub.topic.publish --timeout 540s
最佳答案
不清楚你是如何触发这个函数的,但它看起来像一个“后台函数”,这意味着它需要接受两个参数,即使它们没有被使用:
def main(data, context):
...
有关更多信息,请参阅 https://cloud.google.com/functions/docs/concepts/events-triggers。
关于python - main() 接受 0 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58452034/