带有循环的Python Google Sheets API限制429错误

时间:2018-12-13 15:33:38

标签: python google-sheets-api gspread

我正在运行一个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"
          }
        ]
      }
    ]
  }
}

代码:

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

打印:

['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

1 个答案:

答案 0 :(得分:0)

遇到相同的问题,有一种方法可以按照以下条件限制程序,例如下面的for循环,我们可以将请求的no限制为api并匹配以下条件

time.sleep(10)

关于developers.google.com

  

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