二手产品经理

二手产品经理

THIS IS RENO

Automate - 97~98 days - Learn Python online for 100 days

Record#

  1. 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.
  2. On the 97th day of practice, the task was to create a continuous running process to execute custom code.
  3. The code used the schedule library to create a schedule. The code schedule.every(2).seconds.do(printMe) means to execute the function printMe every * time interval.
  4. To keep the code running continuously, an infinite loop was created using while True, and schedule.run_pending() was added inside the loop body.
  5. 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)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.