嵌套在循环中的Python仅运行一次

时间:2020-09-01 13:16:41

标签: python firebase google-cloud-firestore

我在嵌套的for循环(用于查询中的doc)上遇到了问题,该循环仅运行了一次。它已在news_items中的项目中进行了验证,该项目已被我迭代了10次,而查询循环中的for doc应该进行了9次迭代。当我打印文档时,它会打印9个文档,但是当我试图确定是否检查文档的内容时,它只能运行一次。 (我希望有9 x 10的输出,因为它正在检查从父项到查询文档的项目,但我得到的只是9个输出)。 我试图在堆栈上查看,但发现与我使用的其他编程语言无关,似乎没有任何意义,我看不出为什么这行不通,但可能由于我对Python相当陌生而错过了一些东西(1周)。

def scrape(url):
# GET DATE AT THE TIME OF CRAWL START
today = date.today()
d1 = today.strftime("%d/%m/%Y")
# D2 is used for query only
d2 = today.strftime("%Y%m%d")
# LOAD URL IN DRIVER
driver.get(url)
try:
    news_container = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "FlashNews-Box-Root"))
    )
    # array of items
    news_items = news_container.find_elements_by_class_name("FlashNews-Box-Item")

    refresher_ref = db.collection(u'news').document('sources').collection('refresher_news')

    # query for last article
    query = refresher_ref.order_by(u'article_timestamp', direction=firestore.Query.DESCENDING).limit(10).stream()

    for item in news_items:
        print("News items found: " + str(len(news_items)))
        try:
            # image is optional so we need to try it
            try:
                item_image = item.find_element_by_class_name("FlashNews-Box-ItemImage").find_element_by_tag_name(
                    "img").get_attribute("src")
            except Exception as e:
                item_image = "unavailable"

            # time will be added to the same day as when this was ran, since this will run often and compare
            # article texts, we won't have issue with wrong dates
            item_time = item.find_element_by_class_name("FlashNews-Box-ItemTime").text + " " + d1
            item_time_query_temp = item.find_element_by_class_name("FlashNews-Box-ItemTime").text.replace(":", "")
            # normalize timestamp for sorting
            if len(item_time_query_temp) == 3:
                item_time_query_temp = "0" + item_time_query_temp

            item_time_query = d2 + item_time_query_temp
            item_text = item.find_element_by_class_name("FlashNews-Box-ItemText").text
            item_redirect = item.find_element_by_class_name("FlashNews-Box-ItemText").find_element_by_tag_name(
                "a").get_attribute("href")
            result = {"article_time": item_time, "article_url": item_redirect, "article_image": item_image,
                      "article_text": item_text, "article_timestamp": item_time_query}
            # print(result)
            # save data to firestore - check for last item in firestore, then add this article
            is_new = True

            print("Printing 10x")
            # THIS EXECUTES ONLY ONCE?
            for doc in query:
                # print(str(len(query)))
                current_doc = doc.to_dict()
                # print(current_doc)
                # print(current_doc)
                # print("Iteration: " + current_doc['article_text'])
                # print("Old: " + current_doc["article_text"] + " New: " + item_text)
                if current_doc['article_text'] == item_text:
                    print("Match")
                    # print(current_doc['article_text'] + item_text)
                    # print("Old: " + current_doc['article_text'] + " New: " + item_text)
                else:
                    print("Mismatch")
                    # print(current_doc['article_text'] + item_text)
                    # print("Skipping article as the text exists in last 10")
                # else:
                # print("Old: " + current_doc['article_text'] + " New: " + item_text)
                # print(str(is_new))

            # if is_new:
            # refresher_ref.add(result)
            # print("Adding document")

        except Exception as e:
            print(e)

except Exception as e:
    # HANDLE ERRORS
    print(e)

print("Completed running.")
# quit driver at the end of function run
driver.quit()

1 个答案:

答案 0 :(得分:1)

query不是列表,而是其他一些只能使用一次的可迭代类型(类似于generator)。为了在外部循环中多次使用它,您需要创建一个列表以将内容保存在内存中。例如,

# query for last article
query = refresher_ref.order_by(u'article_timestamp', direction=firestore.Query.DESCENDING).limit(10).stream()

query = list(query)

for item in news_items:
    ...