将数据帧中的值获取到一系列 Web API 请求中



我对Python非常陌生(请善待(,所以我不以Python的方式思考...还。

我需要构建一系列 16 个 Web API 字符串,以便从 Web 源中获取特定位置的天气数据,然后将每个字符串发布到诀窍中的新位置。我正在使用 pandas 数据帧来保存硬编码的位置数据,我想遍历每个数据以获取/发布正确的数据。

import requests, json, pytemperature
import pandas as pd
from pandas.io.json import json_normalize
from datetime import date
#Important information for each weather station
stations = pd.DataFrame([
['EGPD', 'Aberdeen','1099','-2.1981','57.2025'],
['EGUC', 'Aberporth','1109','-4.5594','52.1147'],
['EGAA', 'Belfast','1094','-6.2158','54.6575'],
['EGBB', 'Birmingham','1093','-1.7481','52.4539'],
['EGQM', 'Boulmer','1095','-1.6033','55.4219'],
['EGHH', 'Bournemouth','1104','-1.8425','50.7800'],
['EGGD', 'Bristol','1102','-2.7192','51.3828'],
['EGKK', 'Gatwick','1103','-0.1903','51.1481'],
['EGPF', 'Glasgow','1106','-4.4331','55.8719'],
['EGLL', 'Leeming','1100','-1.5356','54.2925'],
['EGQL', 'Leuchars','1098','-2.8686','56.3731'],
['EGCC', 'Manchester','1105','-2.2750','53.3539'],
['EGDB', 'Plymouth','1108','-4.1058','50.4228'],
['EGPO', 'Stornoway','1101','-6.3311','58.2156'],
['EGXW', 'Waddington','1097','-0.5308','53.1725'],
['EGUW', 'Wattisham','1096','0.9558','52.1269']
],
index=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'],
columns=['Station', 'Name', 'Code', 'Lon', 'Lat'])
#Basic Information for the API request
URL = "https://api.openweathermap.org/data/2.5/weather?"
AUTH = "XXXXXXXXXXXXXXX"
#HOW TO USE THE LAT AND LON FROM THE STATIONS DATAFRAME ?
LAT = "51.1481"
LON = "-0.1903"
#Build the GET query
response = requests.get(URL+"lon="+LON+"&lat="+LAT+"&appid="+AUTH)
data = response.json()
#Flatten the data
data_df = json_normalize(data,sep="_")
#Choose the specific column(s) to post 
MyStationID = stations.iloc[0]['Station'] #Field_1382
MyStation = data_df.iloc[0]['name'] #Field_1383
MyCoords = stations.iloc[0]['Lon'],stations.iloc[0]['Lat'] #Field_1384
MySLCode = stations.iloc[0]['Code'] #Field_1381
MyDate = date.today() #Field_1386
MyTemp = pytemperature.k2c(data_df.iloc[0]['main_temp_min']) #Field_1389
MyMinTemp = pytemperature.k2c(data_df.iloc[0]['main_temp_min']) #Field_1387
MyMaxTemp = pytemperature.k2c(data_df.iloc[0]['main_temp_max']) #Field_1388
#Build the POST query
knackURL = "https://api.knack.com/v1/objects/object_96/records"
payload = '{ 
"field_1386": "' + str(MyDate) + '", 
"field_1382": "' + str(MyStationID) + '", 
"field_1383": "' + str(MyStation) + '", 
"field_1381": "' + str(MySLCode) + '", 
"field_1384": "' + str(MyCoords) + '", 
"field_1389": "' + str(MyTemp) + '", 
"field_1387": "' + str(MyMinTemp) + '", 
"field_1388": "' + str(MyMaxTemp) + '" 
}'
knackHEADERS = {
'X-Knack-Application-Id': "XXXXXXXXXXXXXXX",
'X-Knack-REST-API-Key': "XXXXXXXXXXXXXXX",
'Content-Type': "application/json"
}
response = requests.request("POST", knackURL, data=payload, headers=knackHEADERS)
print(response.text)

希望我想要做的事情是有意义的,但是我知道我对python的思考还不够。对我来说有意义的是遍历工作站数据帧中的每一行,并使用变量来完成获取 API URL 并在发布有效负载中使用。

我欢迎任何意见或建议。

尝试使用zip也许?

for lon, lat in zip(df['Lon'], df['Lat']):
response = requests.get(URL+"lon="+lon+"&lat="+lat+"&appid="+AUTH)
data = response.json()
...

最新更新