简体   繁体   English

从外部读取湿度数据的属性错误 API API

[英]Attribute Error API for reading humidity data from external API

I want to read to humidity data from the API but I keep getting an atribute error.我想从 API 读取湿度数据,但我一直收到属性错误。 Anybody can help, I am new to coding and python.任何人都可以提供帮助,我是编码和 python 的新手。

Error:错误:

Traceback (most recent call last):
  File "C:\Users...HidenForPrivacy", line 27, in <module>
    test.current_humidity()
  File "C:\Users...HidenForPrivacy", line 20, in current_humidity
    response = requests.get(self.url)
AttributeError: 'humidity' object has no attribute 'url'


    
import requests
import json
class humidity():
    def init(self,humidity, url):
        self.humidity = humidity
        self.api_key = "hiddenforprivacy"
        self.lat = "53.5502"
        self.lon = "9.9920"
        self.url = url

    def current_humidity(self):
        response = requests.get(self.url)
        data = json.loads(response.text)
        self.url = "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s&units=metric" % (self.lat, self.lon, self.api_key)
        self.humidity = data["current"]["humidity"]
        print(humidity)

test = humidity()
test.current_humidity()

The problem is that you have not yet set any value to self.url问题是您尚未为 self.url 设置任何值

When you call test = humidity() , you do not call the init(self, humidity, url) method, but the empty __init__(self) method (the constructor in Python is called __init__ ).当你调用test = humidity()时,你调用的不是init(self, humidity, url)方法,而是空的__init__(self)方法(Python 中的构造函数称为__init__ )。 So there the url is not set.所以没有设置 url。

In your code you do set the url in line 22 self.url = "https://api.openweath... , but that happens after you already called response = requests.get(self.url) .在您的代码中,您确实在第 22 行中设置了 url self.url = "https://api.openweath... ,但这发生在您已经调用response = requests.get(self.url)之后。

One solution might be to put the line self.url = "https://api.openweath... before response = requests.get(self.url)一种解决方案可能是在response = requests.get(self.url)之前放置self.url = "https://api.openweath...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从stackoverflow rest api读取数据 - reading data from stackoverflow rest api 表格 API:使用 Python 从电子表格中读取数据 - Sheets API: reading data from spreadsheet with Python 从 Tree View Odoo 11 中的外部 API 获取外部数据 - Get External Data from External API in Tree View Odoo 11 Django 如何从外部 api 数据更新模型对象字段 - Django How to update model object fields from external api data Airflow Operator 从外部 Rest API 中提取数据 - Airflow Operator to pull data from external Rest API 无法从 django 中的外部 API 检索 HTTP Post 数据 - Unable to retrieve HTTP Post data from external API in django 从Django REST API接收数据时出错 - Error in receiving data from the Django REST API 使用 load_table_from_dataframe 方法将数据写入 BigQuery 表错误 - 'str' object 没有属性 'to_api_repr' - Write Data to BigQuery table using load_table_from_dataframe method ERROR - 'str' object has no attribute 'to_api_repr' 使用 django 中的外部 API 数据更新数据库 - Update a database with external API data in django Python AttributeError AsObj' object 没有外部属性 API - Python AttributeError AsObj' object has no attribute with external API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM