且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Adwords API提取数据并将其放入Pandas数据框

更新时间:2023-01-30 20:35:59

因此,如果有人好奇或遇到与我相同的问题,我就能找出自己问题的答案.我必须import io并将adwords查询的输出写到一个名为output的字符串中.然后,我使用seek()方法从头开始,并使用熊猫read_csv进行阅读.

So I was able to find out the answer to my own question if anybody is curious or had the same problem I did. I had to import io and wrote the output from the adwords query to a string that I named output. I then used the seek() method to start from the beginning and read that using pandas read_csv.

from googleads import adwords
import pandas as pd
import numpy as np
import io

# Define output as a string
output = io.StringIO()

# Initialize appropriate service.
adwords_client = adwords.AdWordsClient.LoadFromStorage()

report_downloader = adwords_client.GetReportDownloader(version='v201710')

# Create report query.
report_query = ('''
select Date, HourOfDay, Clicks
from ACCOUNT_PERFORMANCE_REPORT
during LAST_7_DAYS''')

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

df.head()