我需要从Adwords到Python中获取关键字效果报告。我正在使用带有googleads库的服务帐户通过de Adwords API将Python连接到Adwords。在我尝试使用data_downloader.DownloadReportWithAwql下载报告之前,一切似乎都运行顺利。 “ AttributeError:'str'对象没有属性'CreateHttpHeader'”。
我正在使用adwords模块对服务帐户进行身份验证,然后使用相应的adwords客户端调用“ ReportDefinitionService”。之后,我将创建数据下载器对象并构建adwords查询,最后使用Awql下载报告。
我看过:
How To Pull Data From the Adwords API and put into a Pandas Dataframe
https://developers.google.com/adwords/api/docs/samples/python/reporting
https://github.com/googleads/googleads-python-lib/issues/253
https://developers.google.com/adwords/api/docs/reference/v201809/ReportDefinitionService
我没有MCC帐户访问权限(越来越难获得),但是我应该在Google Cloud中进行所有设置。这是我的代码:
from googleads import oauth2
from googleads import adwords
import pandas as pd
import numpy as np
import os
# Initialize the GoogleRefreshTokenClient
oauth2_client = oauth2.GoogleServiceAccountClient("marketing-analytics93839202-038212.json", oauth2.GetAPIScope('adwords'))
# Initialize the Adwords client.
adwords_client = adwords.AdWordsClient(oauth2_client, 'AdWords')
ad_report_service = adwords_client.GetService('ReportDefinitionService',
version='v201809')
# Construct query
data_downloader = adwords_client.GetReportDownloader(version='v201809')
# Create report query.
report_query = (adwords.ReportQueryBuilder()
.Select('CampaignId', 'AdGroupId', 'Id', 'Criteria',
'Conversions', 'Impressions', 'Clicks','Ctr',
'Cost')
.From('KEYWORDS_PERFORMANCE')
.Where('Status').In('ENABLED', 'PAUSED')
.During('LAST_30_DAYS')
.Build())
# Up to this point there are no apparent errors. Now write the output
data_downloader.DownloadReportWithAwql(report_query,
file_format='CSV',client_customer_id='xxx-xxx-xxxx',
skip_report_header=True, skip_column_header=False,
skip_report_summary=True,include_zero_impressions=False)
>> AttributeError: 'str' object has no attribute 'CreateHttpHeader'
我得到AttributeError:'str'对象没有属性'CreateHttpHeader'。我希望查询的报告是熊猫数据框。
答案 0 :(得分:0)
AdWordsClient
的构造函数的签名为
def __init__(self, developer_token, oauth2_client,
user_agent=_DEFAULT_USER_AGENT, soap_impl='zeep',
timeout=3600, **kwargs):
您将oauth2_client
实例用作developer_token
参数,并在OAuth2客户端应该运行的地方传递'AdWords'
:
adwords_client = adwords.AdWordsClient(oauth2_client, 'AdWords')
将开发者令牌添加为adwords.AdWordsClient
的第一个参数后,错误就会消失:
adwords_client = adwords.AdWordsClient(DEV_TOKEN, oauth2_client, 'AdWords')
答案 1 :(得分:0)
要将报告下载到熊猫数据框中,请使用以下方法:
oauth2_client = oauth2.GoogleServiceAccountClient("marketing-analytics93839202-038212.json", oauth2.GetAPIScope('adwords'))
# Initialize the Adwords client.
adwords_client = adwords.AdWordsClient(oauth2_client, 'AdWords')
# Define output as a string
output = io.StringIO()
report_downloader = adwords_client.GetReportDownloader(version='v201809')
# Create report query.
report_query = ('''
SELECT CampaignName, CampaignId, AdGroupId, AdGroupName,QueryMatchTypeWithVariant, Query,Cost,Clicks,Conversions,Impressions
from SEARCH_QUERY_PERFORMANCE_REPORT
WHERE CampaignId IN [''' + str(activeCampaignId) +''']''')
# Write query result to output file
report_downloader.DownloadReportWithAwql(
report_query,
'CSV',
output,
#client_customer_id='xxx-xxx-xxx', # denotes which adw account to pull from
skip_report_header=True,
skip_column_header=False,
skip_report_summary=True,
include_zero_impressions=False)
output.seek(0)
df = pd.read_csv(output)