我试图执行下面的脚本并得到一个JSONDecodeError,这导致我的while循环错误地逃跑。
import csv
import json
import time
import requests
page_count = 1000
price_inc = 10
counter = 0
api_response = None
str_json = None
price = 227
while price <= 1300:
try:
url = 'https://www.bluenile.com/api/public/diamond-search-grid/v2?country=USA&language=en-us¤cy=USD&startIndex=' + str(counter) + '&pageSize=' + str(page_count) + '&minPrice=' + str(price) + '&sortColumn=price&sortDirection=asc&_='
s = requests.session()
api_response = s.get(url, allow_redirects=True).json()
str_json = json.dumps(api_response)
print(price)
price = price + price_inc
s.close()
time.sleep(2)
except ValueError:
print('error')
print(price)
price = price + price_inc
time.sleep(1)
我添加了time.sleep()
,因为我遇到了缓冲错误。在引发JSONDecodeError之前,代码将运行大约5-6次迭代,然后循环将只引发错误,直到手动停止或循环结束。我已经搜索并尝试了我在stackoverflow上可以找到的内容,但还没有找到解决方案。任何帮助,将不胜感激。谢谢!