二手产品经理

二手产品经理

THIS IS RENO

挑戰 - 99天 - 在線學習Python100天

記錄#

  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("⏰ Sending Reminder")
  sendMail() # 將子程序移到已經排定的printMe中

schedule.every(6).hours.do(printMe) # 將間隔更改為每1小時

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

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。