簡體   English   中英

暫停 Python For 循環一天

[英]Pause Python For Loop for a day

我的情況是這樣的:我使用 Selenium Webdriver 抓取網頁,首先它獲取 total_page_items,這是比較容易的部分,因為頁面頂部有一個數字框。

我想知道的是每天只與 200 個這樣的項目互動。 例如,該頁面有 500 萬個項目,我將如何每天點擊這些項目中的 200 個,可能將按鈕狀態保存到列表中,然后第二天繼續接下來的 200 個項目? 我知道計時功能以及如何在特定時間每天運行腳本,但我不知道如何從那里開始。 這是我會使用嵌套循環的情況嗎?

這是我到目前為止的 for 循環,我希望它是有道理的

    daily_items = 200
    counter = 0
    ButtonXpathList = [
          "//div[@id='content']/div/div/div[2]/div/div/ul/li[",
                               1,
                               "]/div/div[3]/button [contains(text(), 'Click')]"
     ]



    for i in range(0, daily_items):

        ButtonXpathList[1]  = ButtonXpathList[1] + (1) #Counts up the string
        ButtonXpathString = "".join(str(x) for x in ButtonXpathList)
        ButtonElement = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, (ButtonXpathString))))
        action.move_to_element(ButtonElement)

        if "Click" in ButtonXpathString: # and ButtonElement.is_displayed():
            ButtonElement.click()
            counter += 1
            print counter, "New Buttons Clicked"
        else:
            driver.execute_script("return arguments[0].scrollIntoView();", ButtonElement)
        time.sleep(2)

    if ButtonXpathList[1] == total_page_items:
        print "You're done here"

您可以使用celery創建任務http://www.celeryproject.org/

我建議你使用 APScheduler。 我做了類似的東西,一個每天早上需要運行一次的刮刀 APScheduler 使用簡單:

from apscheduler.schedulers.background import BackgroundScheduler    

scheduler = BackgroundScheduler()
scheduler.start()
scheduler.add_job(yout_routine, 'interval', days=1)

您還可以使用小時和分鍾間隔:

scheduler.add_job(yout_routine, 'interval', hours=24)

文檔: https : //apscheduler.readthedocs.org/en/latest/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM