from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from email_helper import send_email
import time
# 初始化webdriver
driver = webdriver.Chrome()
# 打开网站
driver.get('https://www.example.com/jobs')
# 等待页面加载完成
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, 'jobs_list')))
# 获取最新职位信息
new_jobs = driver.find_elements_by_css_selector('#jobs_list > li')
new_job_details = []
for job in new_jobs:
title = job.find_element_by_css_selector('.job_title').text
link = job.find_element_by_css_selector('.job_title > a').get_attribute('href')
description = job.find_element_by_css_selector('.job_description').text
new_job_details.append((title, link, description))
# 定义定时任务
def job_notification():
# 这里替换为你的邮箱、密码和收件人邮箱
sender_email = 'your_email@example.com'
password = 'your_password'
receiver_email = 'recipient@example.com'
# 获取最新职位信息
new_jobs = driver.find_elements_by_css_selector('#jobs_list > li')
new_job_details = []
for job in new_jobs:
title = job.find_element_by_css_selector('.job_title').text
link = job.find_element_by_css_selector('.job_title > a').get_attribute('href')
description = job.find_element_by_css_selector('.job_description').text
new_job_details.append((title, link, description))
# 发送邮件
send_email(sender_email, password, receiver_email, new_job_details)
# 执行定时任务
while True:
job_notification()
time.sleep(7200) # 每2小时执行一次
# 关闭webdriver
driver.quit()
在这个代码示例中,我们使用了time.sleep()
来实现定时任务,但是在实际应用中推荐使用sched
模块或者cron
作业调度来实现定时任务的管理。此外,邮件发送部分应该使用一个独立的模块,如上面的email_helper
模块,这样可以提高代码的可维护性和可读性。