AdWords API TargetingIdeaPage服务返回的数据格式是什么?

时间:2016-04-05 17:14:32

标签: python pandas google-adwords

当我使用search volume data and trends through their TargetingIdeaSelector查询AdWords API以获取Python client library时,返回的数据如下所示:

(TargetingIdeaPage){
       totalNumEntries = 1
       entries[] = 
          (TargetingIdea){
             data[] = 
                (Type_AttributeMapEntry){
                   key = "KEYWORD_TEXT"
                   value = 
                      (StringAttribute){
                         Attribute.Type = "StringAttribute"
                         value = "keyword phrase"
                      }
                },
                (Type_AttributeMapEntry){
                   key = "TARGETED_MONTHLY_SEARCHES"
                   value = 
                      (MonthlySearchVolumeAttribute){
                         Attribute.Type = "MonthlySearchVolumeAttribute"
                         value[] = 
                            (MonthlySearchVolume){
                               year = 2016
                               month = 2
                               count = 2900
                            },
                            ...
                            (MonthlySearchVolume){
                               year = 2015
                               month = 3
                               count = 2900
                            },
                      }
                },
          },
     }

这不是JSON,似乎只是一个凌乱的Python列表。将月度数据压缩成具有这样结构的Pandas数据帧的最简单方法是什么?

Keyword         |  Year  | Month | Count
 keyword phrase    2016      2       10

2 个答案:

答案 0 :(得分:4)

输出是一个sudsobject。我发现这个代码可以解决问题:

import suds.sudsobject as sudsobject
import pandas as pd
a = [sudsobject.asdict(x) for x in output]
df = pd.DataFrame(a)
  

附录:这曾经是正确的,但API的新版本(我测试过   201802)现在返回一个zeep.objects。但是,zeep.helpers.serialize_object应该做同样的伎俩。   link

答案 1 :(得分:1)

这是我用来查询TargetingIdeaSelector的完整代码,其中包含requestType STATS,以及我用来将数据解析为可用数据帧的方法;请注意开始“将结果解析为pandas数据帧”的部分,因为这将获取上述问题中给出的输出并将其转换为数据帧。可能不是最快或最好的,但它的工作原理!使用Python 2.7进行测试。

"""This code pulls trends for a set of keywords, and parses into a dataframe.

The LoadFromStorage method is pulling credentials and properties from a
"googleads.yaml" file. By default, it looks for this file in your home
directory. For more information, see the "Caching authentication information"
section of our README.

"""

from googleads import adwords
import pandas as pd


adwords_client = adwords.AdWordsClient.LoadFromStorage()

PAGE_SIZE = 10

# Initialize appropriate service.
targeting_idea_service = adwords_client.GetService(
    'TargetingIdeaService', version='v201601')

# Construct selector object and retrieve related keywords.
offset = 0
stats_selector = {
    'searchParameters': [
        {
            'xsi_type': 'RelatedToQuerySearchParameter',
            'queries': ['donald trump', 'bernie sanders']
        },
        {
        # Language setting (optional).
        # The ID can be found in the documentation:
        #  https://developers.google.com/adwords/api/docs/appendix/languagecodes
            'xsi_type': 'LanguageSearchParameter',
            'languages': [{'id': '1000'}],
        },
        {
            # Location setting
            'xsi_type': 'LocationSearchParameter',
            'locations': [{'id': '1027363'}]  # Burlington,Vermont
        }
    ],
    'ideaType': 'KEYWORD',
    'requestType': 'STATS',
    'requestedAttributeTypes': ['KEYWORD_TEXT', 'TARGETED_MONTHLY_SEARCHES'],
    'paging': {
        'startIndex': str(offset),
        'numberResults': str(PAGE_SIZE)
    }
}

stats_page = targeting_idea_service.get(stats_selector)


##########################################################################
# Parse results to pandas dataframe


stats_pd = pd.DataFrame()

if 'entries' in stats_page:
    for stats_result in stats_page['entries']:
        stats_attributes = {}
        for stats_attribute in stats_result['data']:
            #print (stats_attribute)
            if stats_attribute['key'] == 'KEYWORD_TEXT':
                kt = stats_attribute['value']['value']
            else:
                for i, val in enumerate(stats_attribute['value'][1]):                
                    data = {'keyword': kt,
                            'year': val['year'],
                            'month': val['month'],
                            'count': val['count']}
                    data = pd.DataFrame(data, index = [i])                
                    stats_pd = stats_pd.append(data, ignore_index=True)


print(stats_pd)