I originally intended to send greetings through wechat, but it seems that the web interface of wechat has stopped working, and the ItChat module cannot be used, so I used email instead.
API collaboration is not tiring
The weather API
You can get the weather, we have learned, today just copy, and then modify a little bit
Daily sentence
This interface is provided by Kingsoft Powerword, you can not only listen to a piece of music, but also teach your girlfriend to learn an English sentence. I don’t know how good it would be to send a chicken soup to her heart.
To analyze problems
Train of thought
That’s the idea. It’s super simple.
Start coding step by step…
The import module
We learned about requests and JSON in previous classes, so we don’t need to explain! Please skip.
Smtplib is a mail service, do not need to download, own.
For the weather
Can you believe that you can crawl the local weather using 2 lines of Python? In this lesson, we construct a custom function to get the weather. To make it easier to see the entire code, I’ll wrap it
But the actual runtime is not like the one shown above.
Get a sentence of the day
Call Kingsoft powerword interface to get a sentence of the day.
As shown in the figure above, we only need content In English and Note in Chinese.
Write out the function, use return to format the result as a string, and return.
Some data to use
Because we’re going to use some data in this program, so we’re going to just use the dictionary, construct the data, and add it as we want. The main thing is to save the address of the interface, the name of the city, and the beginning and end of the email.
For example, last is a space, and \r\t is a newline character for text type.
Mail text data
An email needs To be formatted. It needs From To be the sender, To To be the recipient, and Subject To be the Subject. And then I’m going to change the line and add the text. It has to be. Don’t ask why.
I’m going to replace the green part of the picture with my own. Fill in the sender’s email, yourself. Fill in the recipient email address, which is your girlfriend’s email address. And then we add MSG, which is the message that we get using two apis, one is today’s weather, one is the sentence of the day, combined. Add to the message message.
Send E-mail
To send emails, the SMTP service needs to be enabled in the Settings of the email box.
I use email box 163, so I need connect to SMTP mail service of 163.
It’s over. Let’s run it.
Ha ha ha, done
Python complete code
If you need the source code, please reply in the public number background [source], every time I will upload the code there, there is a need, please help yourself. Today’s file is called “s23.py”