python:捕获非基的错误

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

我有下面生成错误的代码:
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”。如何捕获上面的错误?

  1. # gets data from google spreadsheet
  2. def fetch_data_from_google_spreadsheet(SPREADSHEET_ID, google_credentials):
  3. # looks at connecting to the google api service
  4. # If modifying these scopes, delete the file token.pickle.
  5. SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
  6. try :
  7. service = build('sheets', 'v4', credentials=google_credentials)
  8. # Call the Sheets API
  9. sheet = service.spreadsheets()
  10. # Returns all sheets (tabs) in a spread sheet
  11. time.sleep(3)
  12. return_sheet_ID = sheet.get(spreadsheetId=SPREADSHEET_ID, ranges=None, includeGridData=None, x__xgafv=None).execute()
  13. # extracts just the sheets
  14. individual_sheets = return_sheet_ID.get("sheets")
  15. list_of_dict_sheets = []
  16. # loops through each tab and returns the rows within each sheet
  17. for item in individual_sheets:
  18. # returns the sheet names to pass into sheet values
  19. RANGE_NAME = item.get("properties").get("title") + "!" + "1" + ":" + str(item.get("properties").get("gridProperties").get("rowCount"))
  20. time.sleep(3)
  21. # returns the rows within the spreadsheets
  22. list_of_dict_sheets.append (sheet.values().get(spreadsheetId=SPREADSHEET_ID,
  23. range=RANGE_NAME).execute())
  24. df2 = None
  25. # loops through each sheet and returns the details
  26. for each_sheet in list_of_dict_sheets:
  27. df = pd.DataFrame(each_sheet.get("values"))
  28. df = df.rename(columns=df.iloc[0])
  29. df = df.iloc[1:]
  30. df2 = pd.concat([df,df2])
  31. return df2
  32. except Exception as e:
  33. print("Unexpected error:", sys.exc_info()[0])

暂无答案!

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

相关问题