【问题标题】:Gspread quota exceded, error 429 - PythonGspread 配额超出,错误 429 - Python
【发布时间】:2021-12-12 18:50:37
【问题描述】:

我正在做一个机器人,使用 Google Sheets API 将网站的结果放入工作表中。但是我收到了错误 429。我看到有人说要放一个 time.sleep 来处理它,但我不确定我可以把它放在我的代码中的什么地方。请问,有人可以帮忙吗?我是 Python 新手。

from googleapiclient.discovery import build
from google.oauth2 import service_account
from oauth2client.service_account import ServiceAccountCredentials
import gspread

scope = ['https://www.googleapis.com/auth/spreadsheets', "https://www.googleapis.com/auth/drive.file",
         "https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name(r'C:\Users\Camila\Python-projects\avon-chave-sheets.json', scope)
client = gspread.authorize(creds)

result = client.open("Dados_faturados_dia_avon").worksheet("dadosfaturados")

i = 1
while result.cell(i, 1).value != "":
    i = i + 1


result.update_cell(i,1, valores)

错误:

APIError: {'code': 429, 'message': "Quota exceeded for quota metric 'Read requests' and limit 'Read requests per minute per user' of service 'sheets.googleapis.com' for consumer 'project_number:532510513093'.", 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'RATE_LIMIT_EXCEEDED', 'domain': 'googleapis.com', 'metadata': {'quota_limit': 'ReadRequestsPerMinutePerUser', 'consumer': 'projects/532510513093', 'quota_metric': 'sheets.googleapis.com/read_requests', 'service': 'sheets.googleapis.com'}}]}

【问题讨论】:

  • 在您的代码中,您需要捕获错误以可能在 result.update_cell 周围开始,如果它失败然后再试一次。
  • 我对您的脚本有疑问。 1.result.update_cell(i,1, valores)是什么?这是在while循环中吗?或者,这与 while 循环无关? 2.valores是什么?这似乎没有在您的脚本中声明。

标签: python selenium google-api google-oauth google-api-python-client


【解决方案1】:

您需要在代码中捕获错误,然后重试。

我不是 python 开发人员,但这样的事情应该可以帮助您入门。

from googleapiclient.errors import HttpError
try:
   result.update_cell(i,1, valores)
except HttpError as err:
  # If the error is a rate limit or connection error,
  # wait and try again.
  if err.resp.status in [403, 429, 500, 503]:
    time.sleep(5)
  else: raise

【讨论】:

  • 嗨! @DalmTo 非常感谢您的回答,但我在这里尝试并得到了同样的错误:(((你还有其他想法吗?
  • 我不是 python 开发人员,您必须在谷歌上搜索如何捕获错误并使其再次运行。我用 C# 做这个,它可以工作,你只需要抓住它并在它失败时重试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
相关资源
最近更新 更多