【问题标题】:Sending emails with Python based on the condition of a while loop within a script根据脚本中的 while 循环条件使用 Python 发送电子邮件
【发布时间】:2020-12-06 19:57:28
【问题描述】:

我目前正在做一个 Python 项目,其中脚本访问一个网站 (https://service.berlin.de/dienstleistung/120686/),点击链接“Termin berlinweitsuchen 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)

【问题讨论】:

  • 也许你想放一些打印语句并检查它是否进入了 else 循环......
  • 同样在gmail设置中,您是否更改了“允许低安全性应用程序”设置?或者允许不安全的应用程序或类似的东西......
  • 是的,电子邮件设置已更改,我也会尝试打印一些语句,但我怀疑错误是由于代码的逻辑引起的
  • 如果你怀疑是这样,那么我也会尝试编写替代代码...
  • ohhh...我认为问题出在requests.getdriver.refresh。他们没有任何关系。当然,它会刷新您的驱动程序。但是appointmentPage 没有加载到你的驱动程序中,它没有在你的硒驱动程序中打开......它是使用requests 模块发送的,它与硒没有关系。所以刷新后,appointmentPage 仍然具有旧值,因为刷新不起作用......我希望你能明白,否则我可以更详细地解释......

标签: python selenium selenium-webdriver


【解决方案1】:

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

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

while True:
  currentHash = hashlib.sha256(driver.page_source).hexdigest()
  time.sleep(100)
  driver.refresh()
  newHash = hashlib.sha256(driver.page_source).hexdigest()
  if newHash == currentHash:  # Time to compare hashes!
    continue  # If the hashes are different, send email
  else:
    #send mail

【讨论】:

  • 嘿,这是个好主意。我会让脚本运行一段时间,看看它是否有效,然后回复你!
  • 酷!实际上,我没有太多使用硒......所以我希望你明白这个想法,并且可能,你可以尝试围绕同一想法的其他一些方法......让我知道这是否是实际问题......否则我们'将不得不进行更多调试...同时通过仅测试邮件代码来确保邮件正常...
  • 当然可以,我的新代码出错了,我也相应地更新了问题中的代码。我即将探索错误并寻找可能的解决方案
  • 这是一个编码错误..检查这个:stackoverflow.com/questions/16823086/…
  • 以后或明天去看看,因为我现在很累:)
猜你喜欢
  • 2021-12-30
  • 2020-11-03
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
相关资源
最近更新 更多