Python Google Sheets API 使用循环限制 429 错误

cph*_*ill 2 python gspread google-sheets-api

我正在运行一个 python 脚本,该脚本通过 Google Sheets API 和 Gspread 包将特定列从一个 Google Sheet 获取到另一个 Google Sheet,但是429 Error当我将列表推送到新电子表格时遇到了一个问题。此错误与太多请求有关,但我不确定是什么使我的推送多次运行,而不是一次。我的是否有可能worksheet.update_cells(updated_values)被包含在循环中?

错误:

APIError: {
  "error": {
    "code": 429,
    "message": "Quota exceeded for quota group 'WriteGroup' and limit 'USER-100s' of service 'sheets.googleapis.com' for consumer 'project_number:*id*'.",
    "status": "RESOURCE_EXHAUSTED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/*id*/apiui/credential"
          }
        ]
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

代码:

# column names
print(columns) # ['date', 'b_clicks', 'b_cpc']

# store count of column names
gs_columns = []

# count columns
for i in range(0,len(columns)):
    gs_columns.append(i+1)

print(gs_columns) # [1,2,3]

updated_values = []

for col_val, col_name in zip(gs_columns, columns):
    worksheet_range = worksheet.range(1, col_val, 500, col_val); # [row_start, col_start, row_end, col_end]
    print(type(worksheet_range))
    column_data = df_full[col_name].values.tolist();
    for cell, data in zip(worksheet_range, column_data):
        cell.value = data
        updated_values.append(cell)

worksheet.update_cells(updated_values)
print(updated_values)
print(type(updated_values))
print(len(updated_values))
Run Code Online (Sandbox Code Playgroud)

打印:

['date', 'b_clicks', 'b_cpc']
[1, 2, 3]
<class 'list'>
<class 'list'>
<class 'list'>
[<Cell R1C1 1514764800000000000>, <Cell R2C1 1514851200000000000>, <Cell R3C1 1514937600000000000>....<Cell R345C3 3.21>, <Cell R346C3 3.92>]
<class 'list'>
1038
Run Code Online (Sandbox Code Playgroud)

小智 5

遇到同样的问题,有没有一种方法可以根据以下标准限制程序,例如在for循环中,我们可以限制请求到api的数量并匹配以下标准

time.sleep(10)
Run Code Online (Sandbox Code Playgroud)

关于developer.google.com:

Google Sheets API 每个项目每 100 秒限制为 500 个请求,每个用户每 100 秒限制为 100 个请求。读取和写入的限制是单独跟踪的。没有每日使用限制。