首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当通过python插入数据时,如何处理google中超过错误429的配额?

当通过python插入数据时,如何处理google中超过错误429的配额?
EN

Stack Overflow用户
提问于 2021-04-27 07:41:42
回答 1查看 698关注 0票数 3

我对python很陌生,目前正在处理一个自由职业者的任务。在我的项目中,我被赋予了主题名xls文件,它每周一次用新名称更新。我能够为给定的名称刮数据,并通过python将获得的数据插入google中。我现在有5000多个名字在档案里。我以为我的代码已经准备好了,但是在8-10个名字之后,我遇到了429错误,其中指出配额限制超过了。我登录了这个网站,谷歌似乎允许每个项目每100秒有500个请求,每100秒有100个请求,每个用户允许100个请求。考虑到限制因素,我修改了代码并增加了睡眠,这样就不会遇到这个错误,但在这里我似乎有一个误会,按照我的想法,我的代码在循环运行中执行7个请求,在执行睡眠(500)之前运行9个循环,但我仍然面临相同的错误。我肯定我错过了一些非常明显的东西,但经过3天的努力,我自己找出,我正在失去信心,所以任何帮助都是感激的,下面是代码供参考。

代码语言:javascript
运行
复制
import requests
from bs4 import BeautifulSoup
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials
from pandas import ExcelWriter
import time


# define the scope
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

# add credentials to the account
creds = ServiceAccountCredentials.from_json_keyfile_name('/content/drive/MyDrive/ListUpdate.json', scope)

# authorize the clientsheet
client = gspread.authorize(creds)

# get the instance of the Spreadsheet
sheet = client.open('JP_combined_Strip')

# get the first sheet of the Spreadsheet
sheet_instance = sheet.get_worksheet(0)


list_of_lists = sheet_instance.get_all_values()        # req 1
print(len(list_of_lists))

start = int((sheet_instance.cell(2, 1).value))         # req 2 this column is for recording the last row number where this program left off to continue from there next on next run
end = len(list_of_lists) + 1

for i in range(start,end,1):

    ##
    ## code for scraping
    ##
    ##
    ##
    ## scraped data 
    ##

        sheet_instance.update_cell(i, 3, data_1 )        # req 3
        sheet_instance.update_cell(i, 4,data_2)     # req 4
        sheet_instance.update_cell(i, 5, data_3)        # req 5
        sheet_instance.update_cell(i, 6, data_4)                # req 6
        sheet_instance.update_cell(i, 7, data_5)            # req 7
        sheet_instance.update_cell(i, 8, data_6)            # req 8
        sheet_instance.update_cell(i, 9, data_7)        # req 9 (req 7 under loop)
        if i%9 == 0:
            sheet_instance.update_cell(2, 1, i) # req 8 under loop when loop is run9 times = 9 * 7 = 63 requests total
            ## total requests should be 66 in total before each sleep statement is executed which is less than 100 requests as stated in google
            print("sleep")
            time.sleep(500)

代码成功运行,直到第一次睡眠,7记录doo被执行,但下一批失败与此错误。

EN

回答 1

Stack Overflow用户

发布于 2022-03-24 14:09:02

问题是,您只在一定数量的请求之后才能睡觉,而忽略它可能在两者之间的任何地方失败,因此任何API调用都是潜在的失败。

这个问题有许多解决办法。从我的角度来看,最好的方法是将每个调用封装到一个函数中,其中包含了一个try-catch块,并在其中提供了睡眠功能。

代码语言:javascript
运行
复制
import time

def api_call_handler(func):
  # Number of retries
  for i in range(0, 10):
    try:
      return func()
    except Exception as e:
      print(e)
      time.sleep(2 ** i)
  print("The program couldn't connect to the Google Spreadsheet API for 10 times. Give up and check it manually.")
  raise SystemError

此代码的使用示例:

代码语言:javascript
运行
复制
# Before
sheet_instance.update_cell(i, 3, data_1)
# Now
api_call_handler(lambda: sheet_instance.update_cell(i, 3, data_1))

这个解决方案增加了代码的额外结构,并使其冗长,但它是防弹的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67278848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档