【问题标题】:EDITGetting error main() takes 2 positional arguments but 3 were givenEDITGetting error main() 需要 2 个位置参数,但给出了 3 个
【发布时间】:2017-12-27 06:44:32
【问题描述】:

我正在测试 google adwords api 功能(注意:此问题并非直接针对 Google Ad Words API)。

我遇到一个错误,上面写着:main() 接受 2 个位置参数,但给出了 3 个

这是我所做的:

1) 创建了一个包含关键字 Charfield 的关键字模型

2) 创建了一个包含 CharField 类型的表单元素的 KeywordForm

3) 使用 HTML 页面中的表单通过 POST 方法获取关键字

4) 在 POST 之后,URL 被路由到一个 viewkeyword_add,它设置了两个值,即

adwords_client = adwords.AdWordsClient.LoadFromStorage()
ad_group_id = 'XXXXXXXXX` 

另外,它使用关键字模型的值

new_keyword = Keyword.objects.all()

然后它使用函数调用调用位于 python 脚本中的函数

    ad_group_update.main(adwords_client, ad_group_id, new_keyword)

5)python脚本add_keywords中的main函数使用adwords_client、ad_group_id和new_keyword三个参数来执行

当我这样做时会出现以下错误:

1)error while executing main()

除了这个错误,我在代码中还有一个问题:

from googleads import adwords


AD_GROUP_ID = 'INSERT_AD_GROUP_ID_HERE'


def main(client, ad_group_id, keyword):
  # Initialize appropriate service.
  ad_group_criterion_service = client.GetService(
      'AdGroupCriterionService', version='v201710')

  # Construct keyword ad group criterion object.
  keyword1 = {
      'xsi_type': 'BiddableAdGroupCriterion',
      'adGroupId': ad_group_id,
      'criterion': {
          'xsi_type': 'Keyword',
          'matchType': 'BROAD',
          'text': 'MARS'
      },
      # These fields are optional.
      'userStatus': 'PAUSED',
      'finalUrls': {
          'urls': ['http://example.com/keyword']
      }
  }

  keyword2 = {
      'xsi_type': 'NegativeAdGroupCriterion',
      'adGroupId': ad_group_id,
      'criterion': {
          'xsi_type': 'Keyword',
          'matchType': 'EXACT',
          'text': 'pluto'
      }
  }

  # Construct operations and add ad group criteria.
  operations = [
      {
          'operator': 'ADD',
          'operand': keyword1
      },
      {
          'operator': 'ADD',
          'operand': keyword2
      }
  ]
  ad_group_criteria = ad_group_criterion_service.mutate(
      operations)['value']

  # Display results.
  for criterion in ad_group_criteria:
    print(('Keyword ad group criterion with ad group id "%s", criterion id '
           '"%s", text "%s", and match type "%s" was added.'
           % (criterion['adGroupId'], criterion['criterion']['id'],
              criterion['criterion']['text'],
              criterion['criterion']['matchType'])))


if __name__ == '__main__':
  # Initialize client object.
  adwords_client = adwords.AdWordsClient.LoadFromStorage()
  main(adwords_client, AD_GROUP_ID, )

如何使用参数 new_keyword 来更新 Keyword 的文本元素?

【问题讨论】:

    标签: python django google-ads-api


    【解决方案1】:

    在你的代码中

    if __name__ == '__main__':
    # Initialize client object.
        adwords_client = adwords.AdWordsClient.LoadFromStorage()
        main(adwords_client, AD_GROUP_ID, )
    

    您正在传递 2 个参数,但 def main(__, __, __) 需要 3 个参数。

    main(adwords_client, AD_GROUP_ID, )
    

    这就是你得到错误的原因。

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 2020-02-15
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2019-09-21
      • 2019-07-19
      相关资源
      最近更新 更多