用于rendez-vous约会python_selenium_chrome脚本-泓源视野

用于rendez-vous约会python_selenium_chrome脚本

from selenium import webdriver
import time
import smtplib
from email.header import Header
from email.mime.text import MIMEText
driver = webdriver.Chrome('C:\Python\Tools\chromedriver.exe')
#driver.fullscreen_window()
driver.get('http://www.meurthe-et-moselle.gouv.fr/booking/create/22259/1')
driver.implicitly_wait(4)
driver.find_element_by_id("planning22263").click()
#driver.find_element_by_xpath("//*[@id='condition_Booking']//input[@type='checkbox']").click()
time.sleep(5)
driver.find_element_by_xpath("//*[@name='nextButton']").click()
time.sleep(7)
#if driver.current_window_handle ("//*[@h2='Vérification de disponibilité']")driver.refresh()
#else
text = driver.find_element_by_id("inner_Booking").text
print(text)
attribute = driver.find_element_by_id("dialog_Booking").get_attribute('type')
print(attribute)
result = driver.find_element_by_id("dialog_Booking").is_displayed()
print(result)
#def send_email(text,result): #函数中邮箱信息改为自己的
# 第三方 SMTP 服务
mail_host="smtp.gmail.com" #设置服务器
mail_user="****[email protected]" #用户名
mail_pass="****" #口令
mail_use_STARTTLS = True #使用ssl
sender = '****@hotmail.com'
receivers = ['****@gmail.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText(text, 'plain', 'utf-8')
message['From'] = Header("rdv_prfecture", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
text = ("disponile:%s dialog:%s "%(text,result))
try: server = smtplib.SMTP(mail_host,587) server.starttls() # server.connect(mail_host, 587) # 25 为 SMTP 端口号 server.login(mail_user,mail_pass) server.sendmail(sender, receivers, message.as_string()) print ("邮件发送成功")
except smtplib.SMTPException: print ("Error: 无法发送邮件")
time.sleep(10)
#return driver
driver.quit()

注解这是一个windwos环境python3的脚本,关于linux还得做修改,python的selenium模块操作网页自动化,***需要安装驱动webdriver,使用chrome浏览器的话需要安装chrome_webdriver版本要和当前chrome版本对应 83.0.4103.116,chrome_webdriver下载地址为

https://chromedriver.chromium.org/

功能介绍,自动选择预约项目,然后点击获取预约,如果不是disponible的话则发邮件通知non-disponible。添加计划任务到服务器中1小时运行一次!

from selenium import webdriver
import time
import smtplib
from email.header import Header
from email.mime.text import MIMEText
driver = webdriver.Firefox()
driver.get("http://www.meurthe-et-moselle.gouv.fr");
cookie = driver.get_cookies()
time.sleep(5)
#driver.manage().deleteAllCookies();
#driver.findElement(By.xpath("//ul[@id='productListMain']/li[1]/span/a")).click();
#driver.fullscreen_window()
driver.get('http://www.meurthe-et-moselle.gouv.fr/booking/create/22259/1')
driver.implicitly_wait(4)
driver.find_element_by_id("planning22263").click()
#driver.find_element_by_xpath("//*[@id='condition_Booking']//input[@type='checkbox']").click()
time.sleep(5)
driver.find_element_by_xpath("//*[@name='nextButton']").click()
time.sleep(7)
#if driver.current_window_handle ("//*[@h2='Vérification de disponibilité']")driver.refresh()
#else
text = driver.find_element_by_id("inner_Booking").text
print(text)
attribute = driver.find_element_by_id("dialog_Booking").get_attribute('type')
print(attribute)
result = driver.find_element_by_id("dialog_Booking").is_displayed()
print(result)
#def send_email(text,result): #函数中邮箱信息改为自己的
# 第三方 SMTP 服务
mail_host="smtp.gmail.com" #设置服务器
mail_user="***[email protected]" #用户名
mail_pass="***" #口令
mail_use_STARTTLS = True #使用ssl
sender = '[email protected]'
receivers = ['***@gmail.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText(text, 'plain', 'utf-8')
message['From'] = Header("rdv_prfecture", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
text = ("disponile:%s dialog:%s "%(text,result))
try: server = smtplib.SMTP(mail_host,587) server.starttls() # server.connect(mail_host, 587) # 25 为 SMTP 端口号 server.login(mail_user,mail_pass) server.sendmail(sender, receivers, message.as_string()) print ("邮件发送成功")
except smtplib.SMTPException: print ("Error: 无法发送邮件")
time.sleep(10)
#return driver
driver.quit()

linux 环境需要:python3、pip3、 需要安装pip3 install selenium、需要下载Firefox的webdriver驱动,挂到服务器中使用crontab执行sh auto.sh

***前提使用的gmail smtp的话需要

1.smtp端口号不是默认的25,smtp的是587
2.必须设置“允许使用不够安全的应用
最好再此服务器登录gmail账号一遍 通过 二次验证后 允许异地ip登录

cookie问题解决:***目标网址需要手动访问一次 否则非法操作被apache列为ip黑名单

用于rendez-vous约会python_selenium_chrome脚本插图
本文由 泓源视野 作者:admin 发表,其版权均为 泓源视野 所有,文章内容系作者个人观点,不代表 泓源视野 对观点赞同或支持。如需转载,请注明文章来源。
15

发表评论

Protected with IP Blacklist CloudIP Blacklist Cloud
您是第8238643 位访客, 您的IP是:[54.146.97.79]