簡體   English   中英

如何使用 windows 任務調度程序調度 python 腳本以發送 email

[英]how to scheduling a python script to send email using windows task scheduler

I have a python script that scrapes a website and I want to send the data by email every day at 7:00 AM when i run the script the email is send and the function work perfect, but when I tried to use windows Task Scheduler in為了自動化它不運行的腳本。

代碼:

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>

我遇到了問題,在 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 和 Macs 操作系統。 因此,要運行 python 腳本,您應該使用以下格式命令:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM