python天气
By reading this piece, you will learn to create a weather alert system in Python that will send an email notification to multiple recipients when it forecasts that the sky will rain/snow in the next few hours. The email notification contains additional information such as the forecast-ed temperature and humidity.
通过阅读本文,您将学习如何使用Python创建天气警报系统,该系统将在接下来的几个小时预测天空会下雨/下雪时向多个收件人发送电子邮件通知。 电子邮件通知包含其他信息,例如预测的温度和湿度。
There are 4 sections in this tutorial:
本教程分为4个部分:
- Setup 建立
- Implementation实作
- Result结果
- Conclusion结论
Let’s proceed to the next section.
让我们继续下一节。
1.设定 (1. Setup)
天气API(Weather API)
I am going to use ClimaCell’s weather API in getting the forecasted weather data. It provides quite a number of useful and accurate data depends on your needs.
我将使用ClimaCell的天气API来获取天气预报数据。 它根据您的需求提供了大量有用且准确的数据。
At the time of this writing, it covers historical station data up to 4 weeks back as well as daily forecast data up to 15 days ahead. Besides, you can easily implement it as the official documentation provides references for 4 different computer languages:
在撰写本文时,它涵盖了长达4周的历史台站数据以及长达15天的每日天气预报数据。 此外,您可以轻松实现它,因为官方文档提供了4种不同计算机语言的参考:
- JavaScript JavaScript
- Ruby Ruby
- Node节点
- PythonPython
Once you have signed up for it, you will be placed under free tier which allow you to make 1000 call per day and 100 per hour. It should be more than enough for our use case.
注册后,您将被置于免费套餐下,您可以每天拨打1000次,每小时拨打100次。 对于我们的用例来说,它应该绰绰有余。
Head over to the sign up and and register a new account. Once you are done, you should see the following dashboard which outlines the plan details and your call activity.
前往注册并注册一个新帐户。 完成后,您将看到以下仪表盘,其中概述了计划详细信息和您的通话活动。
Take note of the API key as we are going to use it in our code later on.
请注意API密钥,因为稍后我们将在代码中使用它。
电子邮件设定 (Email Configuration)
I am going to use a personal Gmail
account to send the email to myself via SMTP
. In order to use it, you need to change the configuration in the account setting and turn on the Less secure app access
.
我将使用个人Gmail
帐户通过SMTP
将电子邮件发送给自己。 为了使用它,您需要在帐户设置中更改配置,然后打开Less secure app access
。
Head over to the account setting and click on the security menu.
转到帐户设置,然后单击安全菜单。
Turn on the Less secure app access
as follow
如下打开“ Less secure app access
”