Record#
- Practice for 96 days is to request web page content and then summarize the content using openai. Due to key issues, the openai process is skipped.
- Practice for 97 days is to create a continuous running process to execute custom code.
- Use
schedule
to create a process, codeschedule.every(2).seconds.do(printMe)
executes. Code meaning: do something every * time! - To make the code execute continuously, first create an infinite loop, while true add
schedule.run_pending()
in the loop body. - Writing code is still something that requires experience. Adding the code
time.sleep(1)
in the infinite loop can reduce CPU usage from 50% to 0.7%. If it weren't for the teacher, I definitely wouldn't know how to do it.
CODE#
96-day code#
import requests
from bs4 import BeautifulSoup
url = "https://zh.wikipedia.org/wiki/不明飞行物"
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
page = soup.find_all("div",{"class","mw-parser-output"})
for txt in page:
print(txt.text)
97-day code#
import schedule, time, os, smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
password = os.environ['p']
username = os.environ['u']
def sendMail():
email = "Don't forget to take a break!"
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(1).hours.do(printMe) # Changed the interval to every 1 hour
while True:
schedule.run_pending()
time.sleep(1)