记录#
- 96 天的练习是请求网页内容,然后通过 openai 进行内容总结。因为 key 的问题,openai 的过程跳过。
- 97 天的练习是创建一个持续运行的进程,从而执行自定义代码。
- 使用
schedule
创建进行,代码schedule.every(2).seconds.do(printMe)
执行。代码含意:每 * 时间 做 什么! - 要让代码一直执行,先创建无限循环,while ture 在循环体内加入
schedule.run_pending()
- 写代码还是一个需要经验的事情,在无线循环里增加了代码
time.sleep(1)
就能让 CPU 的占用从 50% 下降到 0.7%,如果不是老师傅,肯定是不会干的。
CODE#
96 天 code#
import requests
from bs4 import BeautifulSoup
url = "https://zh.wikipedia.org/wiki/不明飞行物"
respone = requests.get(url)
html = respone.text
soup = BeautifulSoup(html, 'html.parser')
page = soup.find_all("div",{"class","mw-parser-output"})
for txt in page:
print(txt.text)
97 天 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)