二手产品经理

二手产品经理

THIS IS RENO

チャレンジ - 99日 - オンラインでPythonを学ぶ100日

レコード#

  1. 今日の練習は、replit から最新のコンテンツを取得し、コンテンツを replit db に保存することです。
  2. 興味のあるキーワードを設定し、コンテンツにキーワードが含まれている場合はメールを送信します。
  3. メールは 6 時間ごとに送信され、メールの内容はコンテンツのリンクです。
  4. 新しい replit プロジェクトで実行すると、schedule モジュールが見つからないというエラーが表示されますが、別のプロジェクトでは正常に動作します。

CODE#

main.py#

import schedule, time, os, smtplib, requests
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from bs4 import BeautifulSoup

password = os.environ['p']
username = os.environ['u']

url = "https://replit.com/community-hub"
keywords = "API"

def searchkey():
  respone = requests.get(url)
  html = respone.text
  soup = BeautifulSoup(html, 'html.parser')
  title = soup.find_all("a", {"class", "css-epm014"})
  txt2=""
  for txt in title:
    if keywords in txt.text:
      txt1 = f"""
      <a href="{txt['href']}">{txt.text}</a>
      """
      txt2 += txt1
  return txt2
  

def sendMail():
  email = searchkey() 
  server = "smtp.gmail.com" 
  port = 587 
  s = smtplib.SMTP(host = server, port = port) 
  s.starttls() 
  s.login(username, password) 

  msg = MIMEMultipart() 
  msg['To'] = "[email protected]" 
  msg['From'] = username 
  msg['Subject'] = "Take a BREAK" 
  msg.attach(MIMEText(email, 'html'))

  s.send_message(msg) 
  del msg 



def printMe():
  print("⏰ リマインダーを送信中")
  sendMail() # サブルーチンをprintMeに移動しました

schedule.every(6).hours.do(printMe) # インターバルを1時間ごとに変更しました

while True:
  schedule.run_pending()
  #print(searchkey())
  time.sleep(1)

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。