Python + OpenWeatherMap + Flet:“每日”错误

问题描述 投票:0回答:1

我尝试按照教程制作我的小型天气应用程序,但遇到了一个问题:OpenWeatherMap 不会给我当天的名称(从我的时间码观看教程以查看我想要的结果)。

教程:https://www.youtube.com/watch?v=2OEyYTdEah4&t=2921s(时间码56:35)

我的部分代码:

主要的

    def _bot_data():
        _bot_data = []
        #range is a number of days for forecast => limited by the API key
        for index in range(1, 8):
            _bot_data.append(
                Row(
                    spacing=5,
                    alignment="spaceBetween",
                    controls=[
                        Row(
                            expand=1,
                            alignment="start",
                            controls=[
                                Container(
                                    alignment=alignment.center,
                                    content=Text(
                                        #week day display
                                        datetime.datetime.weekday(
                                            datetime.datetime.fromtimestamp(
                                                response['daily'][index]['dt']
                                            )
                                        )
                                    )
                                )
                            ]
                        )
                    ]
                )
            )
        return _bot_data


    def _bottom():
        _bot_column = Column(
            alignment="center",
            horizontal_alignment="center",
            spacing=25,
        )

        for data in _bot_data():
            _bot_column.controls.append(data)


        bottom = Container(
            padding=padding.only(top=280, left=20, right=20, bottom=20),
            content=_bot_column
        )
        return bottom`

请求部分

API_KEY = "my API key"
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
CITY = "Minsk"
COUNTRY = "BY"
days_en = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
days = days_en

url = BASE_URL + "appid=" + API_KEY + "&q=" + CITY
response = requests.get(url).json()

我的图书馆

import datetime
import flet as ft
from flet import *
import requests
import datetime as dt
import math

错误,我得到了什么:

  File "C:\Users\bande\Documents\PycharmProjects\weather_app\main.py", line 290, in _bot_data
    response['daily'][index]['dt']
    ~~~~~~~~^^^^^^^^^
KeyError: 'daily'

我的主要问题是:如何修复我的代码(或只是一个请求)以从 OpenWeatherMap 获取日期名称(例如:今天是星期六,我想获取下一个列表 - 星期六、星期日、星期一等)。完全来自 OpenWeatherMap,而不是来自日期时间库。

当我尝试将请求更改为

responce['dt']
时,我已经得到了我需要的天数,但是在天数列表中每天都重复了很多次。我又心烦又累:(

python datetime python-requests openweathermap flet
1个回答
0
投票

终于找到解决问题的方法了。 OpenWeatnerMap 有多种 API 调用形式。我请求使用城市名称,但要获取几天的天气预报,您需要使用纬度和经度(另一个 API 调用表单)请求。

解决方案应如下所示: 通过城市名称请求经纬度,然后将这些数据写入变量并粘贴到另一个 API 请求表单中。瞧。

API调用示例:

https://api.openweathermap.org/data/2.5/onecall?lat=33.44&lon=-94.04&exclude=hourly,daily&appid={API key}

来源: https://openweathermap.org/api/one-call-api

© www.soinside.com 2019 - 2024. All rights reserved.