Record#
- On the 96th day of practice, the task was to request webpage content and then summarize it using OpenAI. However, the process was skipped due to a key issue with OpenAI.
- On the 97th day of practice, the task was to create a continuous running process to execute custom code.
- The code used the
schedule
library to create a schedule. The codeschedule.every(2).seconds.do(printMe)
means to execute the functionprintMe
every * time interval. - To keep the code running continuously, an infinite loop was created using
while True
, andschedule.run_pending()
was added inside the loop body. - Writing code is a skill that requires experience. By adding the code
time.sleep(1)
inside the infinite loop, the CPU usage decreased from 50% to 0.7%. Only an experienced person would know to do this.
CODE#
Code for the 96th day#
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)
Code for the 97th day#
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)