preface

In our lives, there are many little sisters and little brothers are in a long-distance relationship.

I climb mountains for you, but not to see the scenery. When she has a cold, I can’t say open the door, but drink water.

Although we can not do meticulous care, but we need to know more carefully, teach you to use Python to send the weather forecast to your girlfriend wechat regularly, reduce her illness and discomfort, otherwise you can only say that drinking more hot water is not good!

Realize the effect drawing

conceived

Looking at the requirement description in the background, to implement this feature, we need to solve the following problems:

1. Crawl weather information, which leads to the second problem

2. Dynamically obtain the weather of the specified city

3. Send weather information to designated wechat friends

4. Trigger the crawling action regularly

5. How to associate wechat account

Crawl the weather

1. Determine the URL to crawl and open it in a browser

2. F12 View the layout information of the web page

3. Use xpath or BS4 for node location

4. Get the page information

5, their own splicing crawl to the information

6, write files, write database, send network and so on

code

The above method can obtain the weather information of a city and store it in a dictionary. We need to send it to our friends, and also need to perform string processing on it. The processing code is as follows:

The national city code is shown in the figure below. The code of each city is composed of a 9-digit number, which is used to query the weather information.


Send to a specified friend

City code

Timing task

If you like, you can try to complete this small program by yourself, or choose a similar scene for processing. There are still several areas that need to be optimized in this article.

1, scheduled tasks to make Windows services, which is more elegant, with the boot

2. Send messages to your wechat friends instead of sending emails to the specified mailbox

Any suggestions or personal comments can be left in the comments below