记录#
- 今天的练习是从 replit 抓取最新内容,并把内容保存到 replit db。
- 设定兴趣关键词,如果内容中包含关键词则发送邮件。
- 邮件每 6 小时发送一次,邮件内容是抓取内容的链接。
- 不知道为在新的 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() # Moved the subroutine into printMe which is already scheduled
schedule.every(6).hours.do(printMe) # Changed the interval to every 1 hour
while True:
schedule.run_pending()
#print(searchkey())
time.sleep(1)