【问题标题】:How to use loop to get every element?如何使用循环来获取每个元素?
【发布时间】:2019-09-25 20:47:54
【问题描述】:
def get_first_profile_id(service):
    # Use the Analytics service object to get the first profile id.

    # Get a list of all Google Analytics accounts for this user
    accounts = service.management().accounts().list().execute()

    if accounts.get('items'):
        # Get the first Google Analytics account.
        account = accounts.get('items')[0].get('id')

        # Get a list of all the properties for the first account.
        properties = service.management().webproperties().list(
                accountId=account).execute()

        if properties.get('items'):
            # Get the first property id.
            property = properties.get('items')[0].get('id')

            # Get a list of all views (profiles) for the first property.
            profiles = service.management().profiles().list(
                    accountId=account,
                    webPropertyId=property).execute()

            if profiles.get('items'):
                # return the first view (profile) id.
                return profiles.get('items')[0].get('id')

    return None

大家好,我正在使用 Google Analytics API 通过 Python 提取数据。以上是谷歌提供的示例代码。

我想使用循环从每个帐户下的每个属性中提取每个“视图(配置文件)ID”。但是,上面的代码只提取了第一个帐户的第一个属性的第一个视图(配置文件)ID。

你能分享一些建议吗?感谢您的帮助!

【问题讨论】:

    标签: python python-3.x api loops for-loop


    【解决方案1】:

    为 xxx.get('items')[0] 添加 for loop 以循环访问帐户/属性/配置文件中的所有项目。将配置文件附加到列表中,并在所有循环后返回结果。

    def get_first_profile_id(service):
        # Use the Analytics service object to get the first profile id.
    
        # Get a list of all Google Analytics accounts for the authorized user.
        accounts = service.management().accounts().list().execute()
    
        if accounts.get('items'):
            result = []
            # Loop through Google Analytics account.
            for account_item in  accounts.get('items'):
                account = account_item.get('id')
                # Get a list of all the properties for the account.
                properties = service.management().webproperties().list(
                    accountId=account).execute()
    
                if properties.get('items'):
                    # Loop through property id.
                    for property_item in  properties.get('items'):
                        property = property_item.get('id')
    
                        # Get a list of all views (profiles) for the property.
                        profiles = service.management().profiles().list(
                            accountId=account,
                            webPropertyId=property).execute()
    
                        if profiles.get('items'):
                            # append all view (profile) id in result list.
                            for profile_item in profiles.get('items'):
                                result.append(profile_item.get('id'))
            return result
        return None
    

    【讨论】:

      【解决方案2】:

      替换

      if profiles.get('items'):
       # return the first view (profile) id.    
          return profiles.get('items')[0].get('id')
      

      if profiles.get('items'):
       # return the first view (profile) id.    
          return [x.get('id') for x in profiles.get('items')]
      

      【讨论】:

      • 谢谢@abhilb。您的代码是否返回第一个帐户的第一个属性的所有“视图(配置文件)ID”?我还应该更新“属性”和“帐户”中的某处吗?
      • 是的,我想是的
      • 那么...如何获取所有帐户的所有属性的每个“视图(配置文件)ID”?
      【解决方案3】:

      我还想为初学者提供一个更具可读性的解决方案(不使用列表理解)。

      if profiles.get('items'):
          result = [] 
          for profile in profiles.get('items'):
              result.append(profile.get('id'))
          return result
      

      【讨论】:

      • 谢谢@Pitto。同样的问题...代码是否返回第一个帐户的第一个属性的所有“视图(配置文件)ID”?我还应该更新“属性”和“帐户”中的某处吗?
      • 嗨!请提供您的输入和预期输出,以便我提供更好的帮助。
      猜你喜欢
      • 2020-10-03
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      相关资源
      最近更新 更多