Python &引用;而“真实循环”;不';t使函数再次执行

Python &引用;而“真实循环”;不';t使函数再次执行,python,selenium,web-scraping,while-loop,beautifulsoup,Python,Selenium,Web Scraping,While Loop,Beautifulsoup,这个问题的标题几乎是不言自明的。我当前正在运行以下代码: from selenium import webdriver from bs4 import BeautifulSoup import time import datetime url = "http://www.val-de-marne.gouv.fr/booking/create/4963/1" cookie_banner_selector = "#cookies-banner a:nth-child(2)" guichet_21

这个问题的标题几乎是不言自明的。我当前正在运行以下代码:

from selenium import webdriver
from bs4 import BeautifulSoup
import time
import datetime

url = "http://www.val-de-marne.gouv.fr/booking/create/4963/1"
cookie_banner_selector = "#cookies-banner a:nth-child(2)"
guichet_21 = "//input[@value='5955'][@name='planning']"
guichet_22 = "//input[@value='5968'][@name='planning']"
guichet_24 = "//input[@value='5973'][@name='planning']"
booking_selector = "//input[@value='Etape suivante'][@name='nextButton']"
guichet_buttons = [guichet_21, guichet_22, guichet_24]
name_guichet = ["guichet 21", "guichet 22", "guichet 24"]
guichet_buttonsandnames = zip(guichet_buttons, name_guichet)

browser = webdriver.Safari()
browser.maximize_window()
browser.get(url)
time.sleep(5)
cookie_banner = browser.find_element_by_css_selector(cookie_banner_selector)
browser.execute_script("arguments[0].click();", cookie_banner)
time.sleep(5)


def availability_checker():
    for i, j in guichet_buttonsandnames:
        guichet_selection = browser.find_element_by_xpath(i)
        guichet_selection.click()
        time.sleep(5)
        booking_submit = browser.find_element_by_xpath(booking_selector)
        booking_submit.click()
        innerHTML = browser.execute_script("return document.body.innerHTML")
        soup = BeautifulSoup(innerHTML, 'lxml')
        alert = soup.find('form', id='FormBookingCreate')
        alert_text = alert.text
        message = alert_text.strip()
        if message == "Il n'existe plus de plage horaire libre pour votre demande de rendez-vous. Veuillez recommencer ultérieurement.":
            print("No available appointments in %s" % j)
            time.sleep(5)
            browser.back()
            browser.refresh()
        else:
            print("Appointment available in %s! Hurry and complete the form!" % j)
            break


while True:
    print("Checking for appointment availabilty on %s" % datetime.datetime.now())
    availability_checker()
    print("Checking complete on %s" % datetime.datetime.now())
    time.sleep(20)
    browser.refresh()
为了让功能
可用性\u checker()
每20秒重新启动一次

第一次迭代进行得很顺利,因为我收到了一个通知,告知我正在查看的三个展位是否有预约。然而,20秒过后,我得到的只是
print()
消息,就好像函数没有再次运行一样

你能告诉我发生了什么事,我怎么解决吗

提前感谢。

使用zip()将为您提供一个生成器,并且只生成一次结果。 您可以通过更改以下内容轻松解决此问题:

guichet_buttonsandnames = zip(guichet_buttons, name_guichet)
致:


再次调用函数时,检查i、j计数器是否复位。或者,您可以将行
guichet\u buttonsandnames=zip(guichet\u buttons,name\u guichet)
添加到您的while循环中,以便在每次迭代时生成它。不过,Nic Laforge的解决方案将减少每个循环的计算量!令人惊叹的!谢谢你的快速修复!由于我是Python和Selenium/BeautifulSoup的新手,这个问题让我抓狂。另外,感谢Reedinanger提供的替代解决方案。我一定会记住的。干杯
guichet_buttonsandnames = tuple(zip(guichet_buttons, name_guichet))