python:捕获非基的错误

5f0d552i  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(239)

我有下面生成错误的代码:
GoogleAppClient.errors.httperror:<httperror 429请求时https://sheets.googleapis.com/v4/spreadsheets/1fjivwqu2aaca06dpkoh20zqygyyq8rbzj8xwzgglcs4/values/1.%20forge%20operator%20shift%20form%20-%20v1.12%211%3a200?alt=json返回“超过了服务配额指标‘读取请求’和限制‘每用户每分钟读取请求’的配额”“sheets.googleapis.com”用于消费者“项目编号:1”。详细信息:“[{@type':'type.googleapis.com/google.rpc.errorinfo','reason':'rate_limit_excelled','domain':'googleapis.com','metadata':{'quota_limit':'readrequestsperminuteperuser','projects/1','service':'sheets.googleapis.com','quota_metric':'sheets.googleapis.com/read requests'}]>
我想捕捉错误,以指数方式避免频繁调用api。但是,当我运行脚本时,我不确定如何针对这个特定错误。在“except”行中,我尝试了httperror,429,“googleapiclient.errors.httperror”。如何捕获上面的错误?


# gets data from google spreadsheet

def fetch_data_from_google_spreadsheet(SPREADSHEET_ID, google_credentials):

    # looks at connecting to the google api service 
    # If modifying these scopes, delete the file token.pickle.
    SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

    try : 
        service = build('sheets', 'v4', credentials=google_credentials)

        # Call the Sheets API
        sheet = service.spreadsheets()

        # Returns all sheets (tabs) in a spread sheet

        time.sleep(3)
        return_sheet_ID = sheet.get(spreadsheetId=SPREADSHEET_ID, ranges=None, includeGridData=None, x__xgafv=None).execute()

        # extracts just the sheets 
        individual_sheets = return_sheet_ID.get("sheets")

        list_of_dict_sheets = []

        # loops through each tab and returns the rows within each sheet 
        for item in individual_sheets:
            # returns the sheet names to pass into sheet values
            RANGE_NAME = item.get("properties").get("title") + "!" + "1" + ":" + str(item.get("properties").get("gridProperties").get("rowCount"))
            time.sleep(3)
            # returns the rows within the spreadsheets 
            list_of_dict_sheets.append (sheet.values().get(spreadsheetId=SPREADSHEET_ID,
                                    range=RANGE_NAME).execute())
        df2 = None 
        # loops through each sheet and returns the details  
        for each_sheet in list_of_dict_sheets:
            df = pd.DataFrame(each_sheet.get("values"))
            df = df.rename(columns=df.iloc[0])
            df = df.iloc[1:]
            df2 = pd.concat([df,df2])
        return df2
    except Exception as e:
        print("Unexpected error:", sys.exc_info()[0])

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题