コンテンツにスキップ

Web Scrping

gmail送信 コマンド

import smtplib
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

def send_mail_attach(from_addr, passward, to_address, subject, body, file_path=None):


    stmp_server = "smtp.gmail.com"
    stmp_port = 587
    stmp_user = from_addr
    stmp_password  = passward

    to_address = to_address
    from_address = stmp_user
    subject = subject
    contents = {'body' : body}
    body = """
    <html>
        <body>
            <p>{body}</p>
        </body>
    </html>""".format(**contents)

    filepath = file_path
    filename = os.path.basename(filepath)

    msg = MIMEMultipart()
    msg["Subject"] = subject
    msg["From"] = from_address
    msg["To"] = to_address
    msg.attach(MIMEText(body, "html"))

    with open(filepath, "rb") as f:
        mb = MIMEApplication(f.read())

    mb.add_header("Content-Disposition", "attachment", filename=filename)
    msg.attach(mb)

    s = smtplib.SMTP(stmp_server, stmp_port)
    s.starttls()
    s.login(stmp_user, stmp_password)
    s.sendmail(from_address, to_address, msg.as_string())
    s.quit()

スケジュール実行 コマンド

pip install schedule
import schedule
import time

# 実行job関数
def job():
    print("job実行"
#1分毎のjob実行を登録
schedule.every(1).minutes.do(job)

#1時間毎のjob実行を登録
schedule.every(1).hours.do(job)

#AM11:00のjob実行を登録
schedule.every().day.at("11:00").do(job)

#日曜日のjob実行を登録
schedule.every().sunday.do(job)

#水曜日13:15のjob実行を登録
schedule.every().wednesday.at("13:15").do(job)

# jobの実行監視、指定時間になったらjob関数を実行
while True:
    schedule.run_pending()
    time.sleep(1)
Back to top