【发布时间】:2021-11-12 10:11:17
【问题描述】:
我有作者姓名列表(超过 1k),我的 google api 引用限制是 20k 我想将作者姓名传递到 API 以获取书籍信息。当我测试我的代码时,我得到“429 客户端错误:对 url 的请求太多...”错误,如何在不停止应用程序的情况下减慢运行时间。 (我在 google colab 中使用 Python)
author_List = ["J. K. Rowling", "mark twain","Emily Dickinson"] #there are more around 1k - 2k
author_List = author_List.to_dict()
newList = []
for key in author_List:
author = author_List[key]
newList.append(searchData(author))
print(newList)
def searchData(authorName):
try:
key= "**********************************"
api = f'https://www.googleapis.com/books/v1/volumes?q={author}&key={key}'
response = requests.get(api)
response.raise_for_status()
print(response)
except requests.RequestException as e:
raise sys.exit(e)
Updated Code
author_List = ["J. K. Rowling", "mark twain","Emily Dickinson"]
connGoogleAPI(author_List)
def connGoogleAPI(booksData):
key= "**************************"
books_list = []
col= ['Title', 'Authors', 'published Date', 'Description','ISBN']
books_list.append(col)
res = ""
err = None
with requests.Session() as session:
#err= ""
for Authors in booksData:
params = {"q": Authors,"key": key,"maxResults": 1}
delays = 65 # approximately 1 minute total delay time for any given author
while True:
try:
url = "https://www.googleapis.com/books/v1/volumes?"
response = requests.get(url,params=params)
print(type(response.raise_for_status()))
err = response.raise_for_status()
res = response.json()
break # all good :-)
except Exception as e:
if err.status_code == 429:
#print("******")
if delays <= 0:
raise(e) # we've spent too long delaying
time.sleep(1)
delays -= 1
else:
print("-----=")
raise(e) # some other status code
books_list.append(lookup(res,Authors))
return books_list
【问题讨论】:
-
列表没有 to_dict 属性
标签: python api google-api