【问题标题】:"While True Loop" doesn't cause the function to execute again“While True Loop”不会导致函数再次执行
【发布时间】:2019-08-02 08:00:54
【问题描述】:

问题的标题是不言自明的。我目前正在运行以下代码:

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() 消息,就好像该函数没有再次运行一样。

您能否说明正在发生的事情以及我该如何解决?

提前致谢。

【问题讨论】:

  • 再次调用该函数时,检查 i、j 计数器是否已重置。或者给页面加载时间。

标签: python selenium web-scraping while-loop beautifulsoup


【解决方案1】:

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

guichet_buttonsandnames = zip(guichet_buttons, name_guichet)

收件人:

guichet_buttonsandnames = tuple(zip(guichet_buttons, name_guichet))

【讨论】:

  • 或者您也可以将guichet_buttonsandnames = zip(guichet_buttons, name_guichet) 行添加到您的while 循环中以在每次迭代时生成它。 Nic Laforge 的解决方案虽然每次循环需要更少的计算!
  • 太棒了!感谢 Nic 的快速修复!因为我是 Python 和 Selenium/BeautifulSoup 的新手,所以这个问题让我抓狂。另外,感谢 Reedinationer 提供的替代解决方案。我一定会记住的。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2016-05-16
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2020-10-29
相关资源
最近更新 更多