You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Google Ads API时整数ID列表报错求助

解决思路
  • 循环逻辑错误:你用range(data_clients["id"])是错误的,因为data_clients["id"]是整数列表,range()需要单个整数参数,直接遍历列表里的每个ID即可。
  • 参数类型错误:Google Ads API的customer_id要求传入字符串类型,直接传整数会触发类型报错。无需用chr()或to_bytes()(这两个是把整数转成单个字符/字节,不适合长ID),直接用str()把整数ID转为字符串就可以。
  • 列表初始化位置错误:list_id = []放在循环内部会导致每次循环都重置为空列表,应该移到循环外面。

修正后的代码示例:

# 先初始化存储结果的列表
list_id = []

# 直接遍历ID列表中的每个整数ID
for client_id in data_clients["id"]:
    # 将整数ID转换为字符串类型
    customer_id_str = str(client_id)
    # 构造查询语句
    query = f'''你的查询内容'''
    # 调用API,传入字符串类型的customer_id
    stream = ga_service.search_stream(customer_id=customer_id_str, query=query)
    
    # 处理返回的数据流并收集结果
    for batch in stream:
        for row in batch.results:
            list_id.append(row)

内容的提问来源于stack exchange,提问作者migueltic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 02:50:21