我已经在我的代码中导入并排除了HTTPError,但是我的程序出现时会暂停。我不确定我需要做什么。错误是因为我用完了100秒的Google表格读取api配额,而我申请了更高的限制,我有无限制的每日限制,超出100秒限制,我可以重试相同的代码,它会工作。所以,我希望我的代码做的是在一个简单的时间延迟后重试。但是,我当前的代码暂停了例外。我在这里缺少什么?
代码:
from urllib2 import HTTPError
import MySQLdb
import pandas as pd
import gspread
import datetime
from oauth2client.service_account import ServiceAccountCredentials
import redis
import requests
import pygsheets
import json
import decimal
import time
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
gc2 = pygsheets.authorize(service_file='XXXXXXXXXXXXXXXXXXXXXX.json')
ss2 = gc2.open_by_key('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
worksheets = ss2.worksheets()
Control = 'ON'
while(Control=='ON'):
redishost='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXaws.com'
r = redis.Redis(host=redishost, port=XXXX, db=0)
for wkst in worksheets:
if wkst.title == 'Log' or wkst.title == 'Control':
pass
else:
url_done = True
while(url_done):
try:
print "worksheet: {}".format(wkst.title)
bid_val = wkst.get_value('O2')
if bid_val:
r.hmset("plcmnt:{}".format(wkst.get_value('I2')),{wkst.get_value('J2'):int(bid_val)})
wkst.update_cell('P2',datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
wkst.update_cell('Q2',r.hget("plcmnt:{}".format(wkst.get_value('I2')),"{}".format(wkst.get_value('J2'))))
wkst.update_cell('R2',datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print "completed at {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
url_done = False
except HTTPError as err:
print err
print "failed updating sheet: '{}'".format(wkst.title)
print "retrying in 10 seconds"
time.sleep(10)
url_done = True
Control = 'OFF'
print "Control is {} at {}".format(Control,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
错误:
File "URL_Bid_Setter.py", line 39, in <module>
wkst.update_cell('Q2',r.hget("plcmnt:{}".format(wkst.get_value('I2')),"{}".format(wkst.get_value('J2'))))
File "/usr/local/lib/python2.7/dist-packages/pygsheets/worksheet.py", line 212, in get_value
return self.get_values(addr, addr, include_empty=False)[0][0]
File "/usr/local/lib/python2.7/dist-packages/pygsheets/worksheet.py", line 242, in get_values
value_render=value_render)
File "/usr/local/lib/python2.7/dist-packages/pygsheets/client.py", line 323, in get_range
result = self._execute_request(spreadsheet_id, request, False)
File "/usr/local/lib/python2.7/dist-packages/pygsheets/client.py", line 418, in _execute_request
response = request.execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 844, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 429 when requesting https://sheets.googleapis.com/v4/spreadsheets/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/values/6326%3Angoisao.vn%21I2%3AI2?valueRenderOption=FORMATTED_VALUE&alt=json&majorDimension=ROWS returned "Insufficient tokens for quota 'ReadGroup' and limit 'USER-100s' of service 'sheets.googleapis.com' for consumer 'project_number:XXXXXXXXXXXX'.">
帮助表示赞赏。
答案 0 :(得分:0)
也许是Google课
googleapiclient.errors.HttpError
与异常无关
urllib2.HTTPError
(只是注意到了评论-所以,这确实是解决方案,很好!)