Life is short. I use Python. Here is a python program that runs on a server that sends a daily weather forecast and a daily message to a specified mailbox. Without further ado, let’s take a look at the overall code and walk through it step by step.

import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email.mime.text import MIMEText from email.header import header account =' [email protected]'#** Password = 'XXXXX '#** Not a login password! ** receiver =' XXXXXX @qq.com'#** ** def sentence(): url = "http://open.iciba.com/dsapi/" r = requests.get(url) content = r.json()['content'] note = r.json()['note'] return content,note def weather_spider(): Headers = {} 'XXXX' # camouflage head url = 'http://www.weather.com.cn/weather1d/XXXXX.shtml' # fill in weather the local site res=requests.get(url=url,headers=headers) res.encoding='utf-8' soup=BeautifulSoup(res.text,'html.parser') tem1= soup.find(class_='tem') weather1= soup.find(class_='wea') clothes1=soup.find(class_='li3 hot').find('p') tem=tem1.text weather=weather1.text clothes=clothes1.text return tem,weather,clothes def send_email(tem,weather,clothes,content,note): Qqmail = smtplib.SMTP_SSL(mailhost)## instantiate and connect to SMTP port number. Qqmail.connect (mailhost,465)#** If run on Aliyun server, only port 465, port 25 will be treated as spam. ** qqmail.login(account,password) content= ' '+tem+', '+weather+', '+clothes+' '+content+' '+note message = MIMEText(content, 'plain', *** message[' subject '] = Header(subject, 'utF-8 ') try: Qqmail. sendmail(Account, receiver, message.as_string()) print (' email sent successfully ') except: Quit () def job(): Print (' start a task ') content,note=sentence() tem,weather,clothes= weather_spider() Send_email (tem, weather, thanks, the content, note) print (' task ') schedule. Every () day. Ats (" from "). Do (job) # regularly perform tasks at seven o 'clock. While True: schedule.run_pending() time.sleep(1)# Check the status of the deployment and start executing the task if it is ready.Copy the code

Complete the code

1. Install the required libraries

These three are third-party libraries, which need to be downloaded separately

PIP install request BeautifulSoup4 PIP install BeautifulSoup4 schedule PIP install schdeduileCopy the code

2. Fill in the recipient information

Ccount =' [email protected]'#** Password =' XXXXX '#** Receiver =' XXXXXX @qq.com'#** Receiver =' XXXXXX @qq.com'#** Receiver =' XXXXXX @qq.com'#** Receiver =' XXXXXX @qq.com'#** **Copy the code

QQ email

This password needs to be obtained here: please open https://mail.qq.com/ and log in to your email. Then click the Settings button at the top, select Account Settings, and drop it down to this location.Copy the code

If not, enable POP3 service ()

You will receive an authorization code, which is password.

Define the function

— 1.

def sentence():
    url = "http://open.iciba.com/dsapi/"
    r = requests.get(url)
    content = r.json()['content']
    note = r.json()['note']
    return content,note
Copy the code

— 2(Weather forecast climb)

def weather_spider(): Headers = {} 'XXXX' # # camouflage head url = 'http://www.weather.com.cn/weather1d/101221201.shtml' replacement for their local website res=requests.get(url=url,headers=headers) res.encoding='utf-8' soup=BeautifulSoup(res.text,'html.parser') tem1= soup.find(class_='tem') weather1= soup.find(class_='wea') clothes1=soup.find(class_='li3 hot').find('p') tem=tem1.text weather=weather1.text clothes=clothes1.text return tem,weather,clothesCopy the code

The ink weather

— 3(Send an email)

def send_email(tem,weather,clothes,content,note): Mailhost = 'smtp.qq.com' qqmail = smtplib.smtp_ssl (mailhost) qqmail.connect(mailhost,465)#** Port 25 will be treated as spam. ** qqmail.login(account,password) content= 'today weather: '+tem+', '+weather+', '+clothes+' '+content+' '+note message = MIMEText(content, 'plain', *** message[' subject '] = Header(subject, 'utF-8 ') try: Qqmail.sendmail (Account, receiver, message.as_string()) print (' email sent successfully ') except: print (' email sent failed ') qqmail.quit()Copy the code

— 4(Performing tasks)

def job(): Print (' start a task ') content,note=sentence() tem,weather,clothes= weather_spider Send_email (tem, weather, thanks, the content, note) print (' task ') schedule. Every () day. Ats (" from "). Do (job) # regularly perform tasks at seven o 'clock. While True: schedule.run_pending() time.sleep(1) # Check the status of the deployment and start executing the task if it is ready.Copy the code

– 5 subtotal

At this point the code is done

The script will run as long as the computer is on. Running it locally shows that this is successful. But this is too much trouble, who get up early in the morning to turn on the computer. You can use the server to hang the script remotely and let it run automatically. So here’s how to do it.

Iii. Server

1. Set up the server

Ali Cloud is recommended here, because it is stable and simple compared with other servers.Copy the code

Ali cloud network

Alibaba Cloud has a developer program, which is specifically targeted at students, who can try out free servers for the first time.

I’m using a lightweight application server, which is cheap and practical, with student discounts and other free trials.

Find your server, set your password and remember it.

Remember your IP and account name: Adminstrator

2. Create an FTP site

1 –

Enter the host (IP address), user name (adminstrator), password, and port (default 21).

2 –

Complete the document on the server side

Create FTP site documents

Then the firewall added rules as shown in the following figure

3. Build the Python environment

2. Download the PIP document

Then, install the required libraries in CMD on the server side. For details, refer to step 1 at the beginning.

requests BeautifulSoup4 schedule

conclusion

Click Run, and the program will be running on the server! Send regularly every day (I use vscode to run) creation is not easy, thank you for your support!

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join