Python:在for循环内嵌套的while循环意外中断,没有任何错误

时间:2019-05-09 01:01:06

标签: python loops python-requests google-cloud-firestore

我在for循环中嵌套了一个while循环,该循环运行在从firestore收集的json数组上,该json数组收集股票代码并传递给另一个api,以收集每一分钟的交易数据,并重新放入firestore db中。

在运行循环时,它将通过389条目while循环在第四或第六次(再也没有)出现错误地意外停止。

有人知道为什么会这样吗?我的代码有问题吗?我注意到,如果我将while循环中的限制从389更改为100,它将遍历json数组中的所有公司。但是,如果它是完整的389个条目,它将不会吸引超过四家公司。

无论如何,谢谢您的帮助!

import requests
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime

cred = credentials.Certificate("./serviceAccountKey.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

doc_ref1 = db.collection(u'Quiver').stream()

for doc in doc_ref1:

    symbol = doc.id

    api_url = "https://api.iextrading.com/1.0/stock/{}/chart/1d".format(symbol)

    query_url = api_url

    r = requests.get(query_url)

    if r.status_code != 200: 
        print("Error:", r.status_code)
        continue

    if r.status_code == 404: 
        print("Error:", r.status_code, symbol)
        continue

    json_stock = r.json()

    b = 0

    while b <= 100:
        try:
            date = json_stock[b]['date']

            minute = json_stock[b]['minute']

            label = json_stock[b]['label']

            high = json_stock[b]['high']

            low = json_stock[b]['low']

            average = json_stock[b]['average']

            volume = json_stock[b]['volume']

            notional = json_stock[b]['notional']

            numberOfTrades = json_stock[b]['numberOfTrades']

            marketHigh = json_stock[b]['marketHigh']

            marketLow = json_stock[b]['marketLow']

            marketAverage = json_stock[b]['marketAverage']

            marketVolume = json_stock[b]['marketVolume']

            marketNotional = json_stock[b]['marketNotional']

            marketNumberOfTrades = json_stock[b]['marketNumberOfTrades']

            open = json_stock[b]['open']

            close = json_stock[b]['close']

            marketOpen = json_stock[b]['marketOpen']

            marketClose = json_stock[b]['marketClose']

            changeOverTime = json_stock[b]['changeOverTime']

            marketChangeOverTime = json_stock[b]['marketChangeOverTime']

            doc_ref = db.collection(u'dailies').document(u'{}-{}'.format(minute, symbol))

            doc_ref.set({
                u'date':u'{}'.format(date),
                u'minute':u'{}'.format(minute),
                u'label':u'{}'.format(label),
                u'high':u'{}'.format(high),
                u'average':u'{}'.format(average),
                u'notional':u'{}'.format(notional),
                u'number of trades':u'{}'.format(numberOfTrades),
                u'market high':u'{}'.format(marketHigh),
                u'market low':u'{}'.format(marketLow),
                u'market average':u'{}'.format(marketAverage),
                u'market volume':u'{}'.format(marketVolume),
                u'market notional':u'{}'.format(marketNotional),
                u'market number of trades':u'{}'.format(marketNumberOfTrades),
                u'open':u'{}'.format(open),
                u'close':u'{}'.format(close),
                u'market open':u'{}'.format(marketOpen),
                u'market close':u'{}'.format(marketClose),
                u'change over time':u'{}'.format(changeOverTime),
                u'market change over time':u'{}'.format(marketChangeOverTime)
            })

            print("{} {}:  {}".format(symbol, minute, b))

            b += 1

        except IndexError:
            print("Index Error")
            break

1 个答案:

答案 0 :(得分:0)

您可以使用:

except Exception as errmsg:
        print(errmsg)

并提供更多信息