我的脚本无法提取所有广告详细信息,我没有得到正确的数字

时间:2019-03-28 17:51:15

标签: python facebook-graph-api google-bigquery

我正在使用facebook marketing API提取广告详细信息,然后将其存储在Google BigQuery上,但是显然我的脚本确实忽略了一些广告集,而且我没有找到解决方法!这是我的代码:

在我的python脚本下面找到:

`for delta in range(1000)[20:28]:

  process_date = (datetime.datetime.today()+datetime.timedelta(days=-delta)).strftime('%Y-%m-%d')
  date_compact = process_date.replace('-', '')
  print('processing date %s, delta %s' % (process_date, delta))

  params['time_range']['since'] = process_date
  params['time_range']['until'] = process_date

  stats = AdAccount(ad_account_id).get_insights(
    fields=fields,
    params=params,
  )


  ad_list = list()
  for ad_insight in stats:
    ad_list.append(ad_insight)
  storage_client = storage.Client(project='project')
  bucket = storage_client.get_bucket('project')
  blob = bucket.blob('fb/fb_%s.csv' % date_compact)

  header_line = ','.join(['date']+fields)
  content_lines = [header_line]
  for ad in ad_list:
    ad_line = ','.join([process_date]+['"%s"'%str(ad[field].encode('latin9').decode('latin9')).replace('"', "'") for field in fields])
    content_lines.append(ad_line)
  blob.upload_from_string('\n'.join(content_lines))

  bq_client = bigquery.Client(project='project')
  job_config = bigquery.LoadJobConfig()
  job_config.write_disposition = 'WRITE_TRUNCATE'
  job_config.field_delimiter = ','
  job_config.skip_leading_rows = 1
  job_config.source_format = 'CSV'

  schema = [bigquery.schema.SchemaField(name=field, field_type='STRING') for field in ['date']+fields]
  job_config.schema = schema

  dataset_id = '2_digital'
  destination_table = 'fb_ad_stats_%s' % date_compact
  table_ref = bq_client.dataset(dataset_id).table(destination_table)
  result = bq_client.load_table_from_uri(source_uris='gs://project/fb/fb_%s.csv'%date_compact, destination=table_ref, job_config=job_config).result()

因此,我期望的是所有包含所有详细信息(点击次数,展示次数和支出)的广告和广告系列

0 个答案:

没有答案