问题的标题几乎是不言自明的。我当前正在运行以下代码:
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()
为了使功能availability_checker()
每20秒重新启动一次。
第一次迭代进行得很顺利,因为我确实得到了通知,通知我正在查看的三个展位是否可用。但是,一旦20秒结束,我收到的都是print()
消息,就好像该函数没有再次运行。
能否请您阐明正在发生的事情以及如何解决?
谢谢。
答案 0 :(得分:1)
使用zip()将为您提供一个生成器,并且仅产生一次结果。 您可以通过以下方法轻松解决此问题:
guichet_buttonsandnames = zip(guichet_buttons, name_guichet)
收件人:
guichet_buttonsandnames = tuple(zip(guichet_buttons, name_guichet))