基于脚本中while循环的条件使用Python发送电子邮件

基于脚本中while循环的条件使用Python发送电子邮件,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我目前正在从事一个Python项目,在该项目中,脚本访问一个网站(),单击链接“Termin berlinweit suchen und buchen”,然后继续刷新页面(在指定时间后),直到页面发生更改。通过比较刷新前后的哈希值来检测网站上的更改。如果有变化,我会收到一封电子邮件。问题是网站有明显的变化,但我没有收到电子邮件。该代码是一个工作示例 我试过: from selenium import webdriver from selenium.webdriver.support.ui imp

我目前正在从事一个Python项目,在该项目中,脚本访问一个网站(),单击链接“Termin berlinweit suchen und buchen”,然后继续刷新页面(在指定时间后),直到页面发生更改。通过比较刷新前后的哈希值来检测网站上的更改。如果有变化,我会收到一封电子邮件。问题是网站有明显的变化,但我没有收到电子邮件。该代码是一个工作示例

我试过:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time, hashlib, smtplib, ssl, requests

driver = webdriver.Firefox(executable_path=r'C:\Users\Me\AppData\Local\Programs\Python\Python37\geckodriver.exe')  # Loads Geckodriver.exe
driver.get("https://service.berlin.de/dienstleistung/120686/")  # Loads initial page

appointmentPageLink = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[4]/div[3]/div[1]/div[2]/div[9]/div[1]/p[1]/a[1]")))
driver.execute_script("arguments[0].click();", appointmentPageLink)  # Clicks the link for appointments

while True:
        currentHash = hashlib.sha256(driver.page_source).hexdigest() # Get hash
        time.sleep(100) # Wait
        driver.refresh() # Refresh page
        newHash = hashlib.sha256(driver.page_source).hexdigest() # Get new hash to comapre

        if newHash == currentHash:  # Time to compare hashes!
            continue  # If the hashes are the same, continue
        else: # If the hashes are different, send email
            port = 587  # For starttls
            smtp_server = "smtp.gmail.com"
            sender_email = "OMITTED"  # Enter your address
            receiver_email = "OMITTED"  # Enter receiver address
            password = "OMITTED"  # Enter sender email password
            message = """\
            Subject: New change detected for Anmeldung!

            Visit https://service.berlin.de/dienstleistung/120686/ now!"""  # Add a message

            context = ssl.create_default_context()  # Send the email!
            with smtplib.SMTP(smtp_server, port) as server:
                server.ehlo()  # Can be omitted
                server.starttls(context=context)
                server.ehlo()  # Can be omitted
                server.login(sender_email, password)
                server.sendmail(sender_email, receiver_email, message)
                server.quit()
错误消息:

Traceback (most recent call last):
  File "C:/Users/Me/PycharmProjects/ServiceBerlin/ServiceBEMonitor.py", line 14, in <module>
    currentHash = hashlib.sha256(driver.page_source).hexdigest() # Get hash
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 2780: ordinal not in range(128)
回溯(最近一次呼叫最后一次):
文件“C:/Users/Me/PycharmProjects/ServiceBerlin/ServiceBEMonitor.py”,第14行,在
currentHash=hashlib.sha256(driver.page_source).hexdigest()#获取哈希
UnicodeEncodeError:“ascii”编解码器无法对2780位置的字符u'\xfc'进行编码:序号不在范围内(128)

在while循环的一次迭代中,您使用
请求
(与selenium无关)向所需URL发送get请求,并将其存储在
任命页面
,然后计算其散列,然后刷新驱动程序并计算同一
appointmentPage
上的哈希值,因为
driver.refresh()
刷新驱动程序,而不是
appointmentPage
,后者是来自
requests
库的HTTP请求。因此,在一次迭代中,
currentHash
始终等于
newHash
newHash
currentHash
的值可能在每次迭代中都会更改,但它们在while循环的迭代中始终相等,因此不会发送邮件

现在要解决您的问题,我们首先需要在驱动程序中获取页面的源代码,然后刷新页面,再次获取源代码并检查它们各自的哈希值。因此,也许以下代码可以工作:

为True时:
currentHash=hashlib.sha256(driver.page_source).hexdigest()
时间。睡眠(100)
driver.refresh()
newHash=hashlib.sha256(driver.page_source).hexdigest()
如果newHash==currentHash:#是时候比较哈希了!
继续#如果哈希值不同,请发送电子邮件
其他:
#寄信

在while循环的一次迭代中,您使用
请求
(与selenium无关)向所需URL发送get请求,并将其存储在
任命页面
,然后计算其散列,然后刷新驱动程序并计算同一
appointmentPage
上的哈希值,因为
driver.refresh()
刷新驱动程序,而不是
appointmentPage
,后者是来自
requests
库的HTTP请求。因此,在一次迭代中,
currentHash
始终等于
newHash
newHash
currentHash
的值可能在每次迭代中都会更改,但它们在while循环的迭代中始终相等,因此不会发送邮件

现在要解决您的问题,我们首先需要在驱动程序中获取页面的源代码,然后刷新页面,再次获取源代码并检查它们各自的哈希值。因此,也许以下代码可以工作:

为True时:
currentHash=hashlib.sha256(driver.page_source).hexdigest()
时间。睡眠(100)
driver.refresh()
newHash=hashlib.sha256(driver.page_source).hexdigest()
如果newHash==currentHash:#是时候比较哈希了!
继续#如果哈希值不同,请发送电子邮件
其他:
#寄信

也许您想输入一些打印状态,并检查它是否进入else循环…同样在gmail设置中,您是否更改了“允许低安全性应用程序”设置?或者允许不安全的应用程序或类似的东西…是的,电子邮件设置已更改,我也将尝试打印一些语句,但我怀疑错误是由于代码的逻辑。如果您怀疑是这样,那么我还将尝试编写替代代码…Ohh。。。我认为问题在于
请求.get
驱动程序.refresh
。他们没有关系。当然,这会让你的司机耳目一新。但是
appointmentPage
未加载到您的驱动程序中,它未在您的selenium驱动程序中打开。。。它是使用
请求
模块发送的,与selenium无关。因此,刷新后,
appointmentPage
仍具有旧值,因为刷新不起作用。。。我希望你能理解,否则我可以更详细地解释…也许你想放一些打印状态并检查它是否进入else循环…同样在gmail设置中,你是否更改了“允许低安全性应用程序”设置?或者允许不安全的应用程序或类似的东西…是的,电子邮件设置已更改,我也将尝试打印一些语句,但我怀疑错误是由于代码的逻辑。如果您怀疑是这样,那么我还将尝试编写替代代码…Ohh。。。我认为问题在于
请求.get
驱动程序.refresh
。他们没有关系。当然,这会让你的司机耳目一新。但是
appointmentPage
未加载到您的驱动程序中,它未在您的selenium驱动程序中打开。。。它是使用
请求
模块发送的,与selenium无关。因此,刷新后,
appointmentPage
仍具有旧值,因为刷新不起作用。。。我希望你明白了,否则我可以更详细地解释……嘿,这是个不错的主意。。我将让脚本运行一段时间,看看它是否有效,然后返回给您!酷!事实上,我没怎么用硒。。。所以我希望你能得到这个想法,也许你可以尝试一些其他的方法来实现这个想法。。。让我知道这是否是真正的问题。。。否则我们将不得不调试更多。。。还要通过测试确保邮件正常工作