Frank
Frank

Reputation: 83

main() takes 0 positional arguments but 2 were given

I have the following code

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()

Testing this code in Google Cloud platform gives

Error: function crashed. Details:
main() takes 0 positional arguments but 2 were given

However when deploying this code it does not give an error. Only scheduling this query does not work since it keeps giving the error below.

enter image description here

For deploying i use the following command (which works)

gcloud functions deploy <function> --entry-point main --
runtime python37 --trigger-resource <name> --trigger-event google.pubsub.topic.publish --timeout 540s

Upvotes: 6

Views: 5027

Answers (1)

Dustin Ingram
Dustin Ingram

Reputation: 21520

It's not clear how you're trigging this function, but it seems like a "Background Function", which means that it needs to take two arguments, even if they're unused:

def main(data, context):
   ...

See https://cloud.google.com/functions/docs/concepts/events-triggers for more information.

Upvotes: 6

Related Questions