如何使用windows任务调度器安排python脚本发送电子邮件

2024-10-01 19:29:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用于抓取网站的python脚本,我想每天早上7:00通过电子邮件发送数据,当我运行该脚本时,电子邮件已发送,功能运行良好,但当我尝试使用windows任务调度器自动运行脚本时,它不会运行

代码:

import time
import pandas as pd
from datetime import date

import requests
from bs4 import BeautifulSoup

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.utils import formatdate



def scrap_website():
    soup = BeautifulSoup(
        requests.get("https://www.bayt.com/en/international/jobs/executive-chef-jobs/").content,
        "lxml"
    )

    links = []
    for a in soup.select("h2.m0.t-regular a"):
        if a['href'] not in links:
            links.append("https://www.bayt.com"+ a['href'])
    joineddd = []

    for link in links:
        s = BeautifulSoup(requests.get(link).content, "lxml")
        jobdesc=s.select_one("div[class='card-content is-spaced'] p")

        alldt = [dt.text for dt in s.select("div[class='card-content is-spaced'] dt")]
        dt_Job_location =              alldt[0]
        dt_Job_Company_Industry =      alldt[1]
        dt_Job_Company_Type =          alldt[2]
        if len(alldt[3])>0:
            dt_Job_Job_Role =              alldt[3]
        elif len(dt_Job_Employment_Type)>0:
            dt_Job_Employment_Type =       alldt[4]
                
        alldt.append("link")
        alldt.append("description")
        
        
        alldd = [dd.text for dd in s.select("div[class='card-content is-spaced'] dd")]
        dd_job_location =             alldd[0]
        dd_job_Company_Industry =     alldd[1]
        dd_job_Company_Type =         alldd[2]
        if len(alldd[3])>0:
            dd_job_Job_Role =             alldd[3]
        elif len(dd_job_Employment_Type)>0:
            dd_job_Employment_Type =      alldd[4]
        
        alldd.insert(0,link)
        alldd.insert(1,jobdesc)
        joineddd.append(alldd)    
        print("-" * 80) 
    
    convert_to_dataFrame(joineddd)
    send_email()


def convert_to_dataFrame(joineddd):
    df = pd.DataFrame(joineddd,columns=["link","description","location","Company_Industry","Company_Type","Job_Role","Employment_Type"])
    df_to_excel = df.to_excel(r"F:\\AIenv\web_scrapping\\jobDesc.xlsx", index = False, header=True)


'''send email '''
def send_email():
    today = date.today()
    file = 'F:\\AIenv\web_scrapping\\jobDesc.xlsx'
    username='xxxxxxx'
    password='xxxxxxx'
    send_from = 'xxxxxxxxxxx'
    send_to = 'xxxxxxxxxxxxxx'
    Cc = 'recipient'
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = send_to
    msg['Cc'] = Cc
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = 'Hello, This is a test mail {}'.format(today)
    server = smtplib.SMTP('smtp.gmail.com')
    port = '587'
    fp = open(file, 'rb')
    part = MIMEBase('application','vnd.ms-excel')
    part.set_payload(fp.read())
    fp.close()
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment', filename='jobs Description--{}'.format(today))
    msg.attach(part)
    smtp = smtplib.SMTP('smtp.gmail.com')
    smtp.ehlo()
    smtp.starttls()
    smtp.login(username,password)
    smtp.sendmail(send_from, send_to.split(',') + msg['Cc'].split(','), msg.as_string())
    smtp.quit()
    print('Mail Sent')

if __name__ == "__main__":
    scrap_website()
   

在windows任务计划程序中:

我按照这些步骤创建触发器以运行脚本

任务计划程序:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-02-02T14:33:03.1578212</Date>
    <Author>DESKTOP-LPD1575\LT GM</Author>
    <URI>\job_desc scheduled email</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2021-03-02T07:30:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-422056822-2861570755-2809137930-1002</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>true</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>F:\AIenv\web_scrapping\job_desc_email.py</Command>
    </Exec>
  </Actions>
</Task>

Tags: tofromimportsendtrueemailtypedt
1条回答
网友
1楼 · 发布于 2024-10-01 19:29:55

我遇到了一个问题,在Exec标记中,必须将完整的python路径添加到命令中

当前代码:

 <Exec>
  <Command>F:\AIenv\web_scrapping\job_desc_email.py</Command>
</Exec>

推荐代码:

假设C:\python39\python.exe是python的路径

<Exec>
  <Command>C:\\python39\\python.exe F:\\AIenv\\web_scrapping\\job_desc_email.py</Command>
</Exec>

因为在Windows操作系统中,.py文件与python.exe(如Linux&;苹果操作系统。因此,要运行python脚本,应使用以下format命令:

{path-to-your-python.exe} {path-to-your-python-script}

相关问题 更多 >

    热门问题