[英]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.