buffoon
Posts: 1
Joined: Sun May 09, 2021 8:34 pm

Openweathermap chance of rain

Sun May 09, 2021 8:39 pm

This is a very beginner question - apologies! I'm using python to get data from the Openweathermap API. I can get all the results by making a request from https://api.openweathermap.org/data/2.5 ... 081&appid=

My question is, using JSON and Python, how do I get access to the first instance of the value 'pop' from the response? I've had a google and can't work it out :oops:

Thanks

ghp
Posts: 1964
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Openweathermap chance of rain

Mon May 10, 2021 2:53 pm

When you have queried the URL (I used my own API key), you get a lengthy string of data, encoded in json.
I placed this in the 'd'- variable. Then parse as json, then access data.

Code: Select all

import json
d = """
{"lat":-41.2111,"lon":174.9081,"timezone":"Pacific/Auckland","timezone_offset":43200,"current":{"dt":1620657017,"sunrise":1620674240,"sunset":1620710175,"temp":290.4,"feels_like":290.48,"pressure":1019,"humidity":88,"dew_point":288.4,"uvi":0,"clouds":90,"visibility":10000,"wind_speed":10.8,"wind_deg":340,"wind_gust":16.46,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}]},"minutely":[{"dt":1620657060,"precipitation":0},{"dt":1620657120,"precipitation":0},{"dt":1620657180,"precipitation":0},{"dt":1620657240,"precipitation":0},{"dt":1620657300,"precipitation":0},{"dt":1620657360,"precipitation":0},{"dt":1620657420,"precipitation":0},{"dt":1620657480,"precipitation":0},{"dt":1620657540,"precipitation":0},{"dt":1620657600,"precipitation":0},{"dt":1620657660,"precipitation":0},{"dt":1620657720,"precipitation":0},{"dt":1620657780,"precipitation":0},{"dt":1620657840,"precipitation":0},{"dt":1620657900,"precipitation":0},{"dt":1620657960,"precipitation":0},{"dt":1620658020,"precipitation":0},{"dt":1620658080,"precipitation":0},{"dt":1620658140,"precipitation":0},{"dt":1620658200,"precipitation":0},{"dt":1620658260,"precipitation":0},{"dt":1620658320,"precipitation":0},{"dt":1620658380,"precipitation":0},{"dt":1620658440,"precipitation":0},{"dt":1620658500,"precipitation":0},{"dt":1620658560,"precipitation":0},{"dt":1620658620,"precipitation":0},{"dt":1620658680,"precipitation":0},{"dt":1620658740,"precipitation":0},{"dt":1620658800,"precipitation":0},{"dt":1620658860,"precipitation":0},{"dt":1620658920,"precipitation":0},{"dt":1620658980,"precipitation":0},{"dt":1620659040,"precipitation":0},{"dt":1620659100,"precipitation":0},{"dt":1620659160,"precipitation":0},{"dt":1620659220,"precipitation":0},{"dt":1620659280,"precipitation":0},{"dt":1620659340,"precipitation":0},{"dt":1620659400,"precipitation":0},{"dt":1620659460,"precipitation":0},{"dt":1620659520,"precipitation":0},{"dt":1620659580,"precipitation":0},{"dt":1620659640,"precipitation":0},{"dt":1620659700,"precipitation":0},{"dt":1620659760,"precipitation":0},{"dt":1620659820,"precipitation":0},{"dt":1620659880,"precipitation":0},{"dt":1620659940,"precipitation":0},{"dt":1620660000,"precipitation":0},{"dt":1620660060,"precipitation":0},{"dt":1620660120,"precipitation":0},{"dt":1620660180,"precipitation":0},{"dt":1620660240,"precipitation":0},{"dt":1620660300,"precipitation":0},{"dt":1620660360,"precipitation":0},{"dt":1620660420,"precipitation":0},{"dt":1620660480,"precipitation":0},{"dt":1620660540,"precipitation":0},{"dt":1620660600,"precipitation":0},{"dt":1620660660,"precipitation":0}],"hourly":[{"dt":1620655200,"temp":290.3,"feels_like":290.39,"pressure":1019,"humidity":89,"dew_point":288.47,"uvi":0,"clouds":92,"visibility":10000,"wind_speed":7.81,"wind_deg":335,"wind_gust":15.38,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.12},{"dt":1620658800,"temp":290.4,"feels_like":290.48,"pressure":1019,"humidity":88,"dew_point":288.4,"uvi":0,"clouds":90,"visibility":10000,"wind_speed":8.2,"wind_deg":334,"wind_gust":15.96,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.08},{"dt":1620662400,"temp":290.29,"feels_like":290.38,"pressure":1019,"humidity":89,"dew_point":288.46,"uvi":0,"clouds":92,"visibility":10000,"wind_speed":8.68,"wind_deg":334,"wind_gust":16.68,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.08},{"dt":1620666000,"temp":290.16,"feels_like":290.24,"pressure":1019,"humidity":89,"dew_point":288.33,"uvi":0,"clouds":93,"visibility":10000,"wind_speed":8.95,"wind_deg":336,"wind_gust":17.31,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.08},{"dt":1620669600,"temp":290.01,"feels_like":290.08,"pressure":1018,"humidity":89,"dew_point":288.19,"uvi":0,"clouds":95,"visibility":10000,"wind_speed":9.26,"wind_deg":339,"wind_gust":18.19,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.08},{"dt":1620673200,"temp":289.97,"feels_like":289.98,"pressure":1017,"humidity":87,"dew_point":287.8,"uvi":0,"clouds":98,"visibility":10000,"wind_speed":9.78,"wind_deg":340,"wind_gust":18.72,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.05},{"dt":1620676800,"temp":289.96,"feels_like":289.94,"pressure":1016,"humidity":86,"dew_point":286.94,"uvi":0.13,"clouds":100,"visibility":10000,"wind_speed":9.55,"wind_deg":343,"wind_gust":19.02,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"pop":0.07},{"dt":1620680400,"temp":290.11,"feels_like":290.08,"pressure":1016,"humidity":85,"dew_point":286.85,"uvi":0.57,"clouds":100,"visibility":10000,"wind_speed":9.98,"wind_deg":346,"wind_gust":19.74,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"pop":0.03},{"dt":1620684000,"temp":289.97,"feels_like":289.98,"pressure":1016,"humidity":87,"dew_point":287.09,"uvi":0.44,"clouds":100,"visibility":10000,"wind_speed":10.45,"wind_deg":342,"wind_gust":20.49,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.27,"rain":{"1h":0.34}},{"dt":1620687600,"temp":289.55,"feels_like":289.65,"pressure":1016,"humidity":92,"dew_point":287.52,"uvi":0.7,"clouds":100,"visibility":10000,"wind_speed":10.53,"wind_deg":337,"wind_gust":20.14,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.36,"rain":{"1h":0.68}},{"dt":1620691200,"temp":289.84,"feels_like":289.99,"pressure":1015,"humidity":93,"dew_point":287.94,"uvi":0.84,"clouds":100,"visibility":10000,"wind_speed":11.05,"wind_deg":333,"wind_gust":19.63,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.41,"rain":{"1h":0.39}},{"dt":1620694800,"temp":289.92,"feels_like":290.08,"pressure":1014,"humidity":93,"dew_point":288.19,"uvi":1.21,"clouds":100,"visibility":9746,"wind_speed":11.25,"wind_deg":330,"wind_gust":19.61,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.41,"rain":{"1h":0.19}},{"dt":1620698400,"temp":290.04,"feels_like":290.21,"pressure":1013,"humidity":93,"dew_point":288.28,"uvi":0.93,"clouds":100,"visibility":10000,"wind_speed":11.1,"wind_deg":329,"wind_gust":19.56,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.36,"rain":{"1h":0.13}},{"dt":1620702000,"temp":290.05,"feels_like":290.22,"pressure":1013,"humidity":93,"dew_point":288.33,"uvi":0.53,"clouds":99,"visibility":9874,"wind_speed":11.07,"wind_deg":330,"wind_gust":19.56,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"pop":0.36},{"dt":1620705600,"temp":290.08,"feels_like":290.28,"pressure":1012,"humidity":94,"dew_point":288.35,"uvi":0.1,"clouds":100,"visibility":9333,"wind_speed":11.01,"wind_deg":330,"wind_gust":19.34,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"pop":0.36},{"dt":1620709200,"temp":290.04,"feels_like":290.27,"pressure":1012,"humidity":95,"dew_point":288.5,"uvi":0,"clouds":100,"visibility":8235,"wind_speed":10.46,"wind_deg":328,"wind_gust":18.33,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.4,"rain":{"1h":0.12}},{"dt":1620712800,"temp":290.04,"feels_like":290.27,"pressure":1012,"humidity":95,"dew_point":288.51,"uvi":0,"clouds":100,"visibility":6820,"wind_speed":9.93,"wind_deg":329,"wind_gust":17.63,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.4,"rain":{"1h":0.32}},{"dt":1620716400,"temp":290.08,"feels_like":290.34,"pressure":1012,"humidity":96,"dew_point":288.71,"uvi":0,"clouds":100,"visibility":7545,"wind_speed":9.24,"wind_deg":330,"wind_gust":16.71,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.36,"rain":{"1h":0.18}},{"dt":1620720000,"temp":290.17,"feels_like":290.38,"pressure":1012,"humidity":94,"dew_point":288.57,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":8.73,"wind_deg":334,"wind_gust":16.7,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.36,"rain":{"1h":0.11}},{"dt":1620723600,"temp":290.03,"feels_like":290.25,"pressure":1013,"humidity":95,"dew_point":288.52,"uvi":0,"clouds":100,"visibility":8522,"wind_speed":8.23,"wind_deg":331,"wind_gust":16.12,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.28},{"dt":1620727200,"temp":289.99,"feels_like":290.18,"pressure":1013,"humidity":94,"dew_point":288.44,"uvi":0,"clouds":100,"visibility":8547,"wind_speed":7.83,"wind_deg":328,"wind_gust":15.83,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.28},{"dt":1620730800,"temp":289.99,"feels_like":290.18,"pressure":1013,"humidity":94,"dew_point":288.3,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.28,"wind_deg":327,"wind_gust":15.34,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.31},{"dt":1620734400,"temp":289.89,"feels_like":290.05,"pressure":1013,"humidity":93,"dew_point":288.13,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":6.94,"wind_deg":323,"wind_gust":15.13,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.31},{"dt":1620738000,"temp":289.73,"feels_like":289.9,"pressure":1013,"humidity":94,"dew_point":288.1,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":6.24,"wind_deg":312,"wind_gust":14.08,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.53,"rain":{"1h":0.17}},{"dt":1620741600,"temp":287.39,"feels_like":287.32,"pressure":1014,"humidity":94,"dew_point":285.76,"uvi":0,"clouds":93,"visibility":10000,"wind_speed":4.57,"wind_deg":208,"wind_gust":9.71,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.69,"rain":{"1h":0.58}},{"dt":1620745200,"temp":285.7,"feels_like":285.41,"pressure":1016,"humidity":92,"dew_point":283.64,"uvi":0,"clouds":94,"visibility":10000,"wind_speed":5.63,"wind_deg":176,"wind_gust":9.1,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.67,"rain":{"1h":0.31}},{"dt":1620748800,"temp":285.2,"feels_like":284.86,"pressure":1016,"humidity":92,"dew_point":283.21,"uvi":0,"clouds":96,"visibility":10000,"wind_speed":6.15,"wind_deg":173,"wind_gust":9.54,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.81,"rain":{"1h":0.17}},{"dt":1620752400,"temp":284.58,"feels_like":284.13,"pressure":1018,"humidity":90,"dew_point":282.37,"uvi":0,"clouds":97,"visibility":10000,"wind_speed":6.67,"wind_deg":171,"wind_gust":10.7,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.9,"rain":{"1h":0.2}},{"dt":1620756000,"temp":284.28,"feels_like":283.77,"pressure":1019,"humidity":89,"dew_point":281.84,"uvi":0,"clouds":97,"visibility":10000,"wind_speed":7.2,"wind_deg":170,"wind_gust":11.63,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.99,"rain":{"1h":0.2}},{"dt":1620759600,"temp":284.12,"feels_like":283.62,"pressure":1020,"humidity":90,"dew_point":281.82,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.8,"wind_deg":173,"wind_gust":12.25,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.64,"rain":{"1h":0.16}},{"dt":1620763200,"temp":283.77,"feels_like":283.16,"pressure":1021,"humidity":87,"dew_point":281.05,"uvi":0.04,"clouds":100,"visibility":10000,"wind_speed":8.08,"wind_deg":170,"wind_gust":12.28,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.68,"rain":{"1h":0.18}},{"dt":1620766800,"temp":283.63,"feels_like":283.01,"pressure":1022,"humidity":87,"dew_point":280.84,"uvi":0.19,"clouds":100,"visibility":10000,"wind_speed":7.76,"wind_deg":167,"wind_gust":11.88,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.69,"rain":{"1h":0.15}},{"dt":1620770400,"temp":283.48,"feels_like":282.81,"pressure":1023,"humidity":86,"dew_point":280.52,"uvi":0.27,"clouds":100,"visibility":10000,"wind_speed":7.59,"wind_deg":164,"wind_gust":11.5,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"pop":0.73},{"dt":1620774000,"temp":283.13,"feels_like":279.67,"pressure":1023,"humidity":86,"dew_point":280.2,"uvi":0.44,"clouds":100,"visibility":10000,"wind_speed":8.49,"wind_deg":159,"wind_gust":12.83,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.89,"rain":{"1h":0.47}},{"dt":1620777600,"temp":282.82,"feels_like":279.59,"pressure":1024,"humidity":82,"dew_point":279.18,"uvi":0.52,"clouds":100,"visibility":10000,"wind_speed":7.25,"wind_deg":148,"wind_gust":12.31,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.99,"rain":{"1h":0.68}},{"dt":1620781200,"temp":283.02,"feels_like":279.95,"pressure":1023,"humidity":81,"dew_point":279.25,"uvi":0.87,"clouds":100,"visibility":10000,"wind_speed":6.9,"wind_deg":141,"wind_gust":12.14,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.64,"rain":{"1h":1}},{"dt":1620784800,"temp":282.99,"feels_like":279.95,"pressure":1024,"humidity":83,"dew_point":279.44,"uvi":0.67,"clouds":100,"visibility":8023,"wind_speed":6.76,"wind_deg":140,"wind_gust":11.94,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"pop":0.74,"rain":{"1h":1.61}},{"dt":1620788400,"temp":282.73,"feels_like":279.5,"pressure":1024,"humidity":85,"dew_point":279.63,"uvi":0.38,"clouds":100,"visibility":10000,"wind_speed":7.16,"wind_deg":137,"wind_gust":12.83,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"pop":0.75,"rain":{"1h":1.7}},{"dt":1620792000,"temp":282.87,"feels_like":279.74,"pressure":1025,"humidity":83,"dew_point":279.41,"uvi":0.29,"clouds":100,"visibility":10000,"wind_speed":6.98,"wind_deg":135,"wind_gust":12.65,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.74,"rain":{"1h":0.74}},{"dt":1620795600,"temp":282.86,"feels_like":279.65,"pressure":1025,"humidity":81,"dew_point":279.07,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.25,"wind_deg":135,"wind_gust":13.05,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"pop":0.74,"rain":{"1h":0.76}},{"dt":1620799200,"temp":282.72,"feels_like":279.46,"pressure":1026,"humidity":80,"dew_point":278.75,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.27,"wind_deg":136,"wind_gust":13.02,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.74,"rain":{"1h":0.52}},{"dt":1620802800,"temp":282.84,"feels_like":279.55,"pressure":1026,"humidity":79,"dew_point":278.69,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.51,"wind_deg":133,"wind_gust":13.17,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.61,"rain":{"1h":0.1}},{"dt":1620806400,"temp":282.85,"feels_like":279.43,"pressure":1026,"humidity":79,"dew_point":278.69,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":8.01,"wind_deg":134,"wind_gust":13.65,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.57},{"dt":1620810000,"temp":282.83,"feels_like":279.44,"pressure":1027,"humidity":77,"dew_point":278.24,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.87,"wind_deg":133,"wind_gust":13.68,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"pop":0.52},{"dt":1620813600,"temp":282.82,"feels_like":279.53,"pressure":1028,"humidity":74,"dew_point":277.8,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.49,"wind_deg":135,"wind_gust":12.86,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.54,"rain":{"1h":0.3}},{"dt":1620817200,"temp":282.56,"feels_like":279.19,"pressure":1028,"humidity":73,"dew_point":277.33,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.51,"wind_deg":137,"wind_gust":12.85,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.62,"rain":{"1h":0.89}},{"dt":1620820800,"temp":282.04,"feels_like":278.58,"pressure":1028,"humidity":77,"dew_point":277.4,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.29,"wind_deg":136,"wind_gust":12.75,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.62,"rain":{"1h":0.8}},{"dt":1620824400,"temp":281.91,"feels_like":278.46,"pressure":1028,"humidity":78,"dew_point":277.59,"uvi":0,"clouds":100,"visibility":10000,"wind_speed":7.11,"wind_deg":128,"wind_gust":12.42,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"pop":0.6,"rain":{"1h":0.11}}],"daily":[{"dt":1620691200,"sunrise":1620674240,"sunset":1620710175,"moonrise":1620670740,"moonset":1620709380,"moon_phase":0.98,"temp":{"day":289.84,"min":289.55,"max":290.4,"night":289.99,"eve":290.04,"morn":290.01},"feels_like":{"day":289.99,"night":290.08,"eve":290.27,"morn":290.08},"pressure":1015,"humidity":93,"dew_point":287.94,"wind_speed":11.25,"wind_deg":330,"wind_gust":20.49,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":100,"pop":0.41,"rain":2.46,"uvi":1.21},{"dt":1620777600,"sunrise":1620760700,"sunset":1620796513,"moonrise":1620760800,"moonset":1620797400,"moon_phase":0,"temp":{"day":282.82,"min":282.56,"max":289.89,"night":282.56,"eve":282.72,"morn":284.28},"feels_like":{"day":279.59,"night":283.77,"eve":279.46,"morn":283.77},"pressure":1024,"humidity":82,"dew_point":279.18,"wind_speed":8.49,"wind_deg":159,"wind_gust":15.13,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":100,"pop":0.99,"rain":10.89,"uvi":0.87},{"dt":1620864000,"sunrise":1620847160,"sunset":1620882853,"moonrise":1620850860,"moonset":1620885660,"moon_phase":0.04,"temp":{"day":284.03,"min":281.73,"max":284.44,"night":284.32,"eve":284.44,"morn":282.47},"feels_like":{"day":283.26,"night":279.18,"eve":284,"morn":279.18},"pressure":1026,"humidity":80,"dew_point":280.15,"wind_speed":7.29,"wind_deg":136,"wind_gust":12.81,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":97,"pop":0.91,"rain":4.11,"uvi":1.84},{"dt":1620950400,"sunrise":1620933620,"sunset":1620969194,"moonrise":1620940860,"moonset":1620974220,"moon_phase":0.07,"temp":{"day":286.02,"min":283,"max":286.26,"night":284.66,"eve":284.87,"morn":284.92},"feels_like":{"day":285.74,"night":284.58,"eve":284.55,"morn":284.58},"pressure":1015,"humidity":91,"dew_point":284,"wind_speed":4.32,"wind_deg":166,"wind_gust":7.79,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":96,"pop":0.69,"rain":1.15,"uvi":2.32},{"dt":1621036800,"sunrise":1621020079,"sunset":1621055537,"moonrise":1621030740,"moonset":1621063260,"moon_phase":0.1,"temp":{"day":288.89,"min":285.55,"max":288.92,"night":286.15,"eve":286.98,"morn":286.83},"feels_like":{"day":288.66,"night":286.76,"eve":286.4,"morn":286.76},"pressure":1001,"humidity":82,"dew_point":285.24,"wind_speed":9.09,"wind_deg":312,"wind_gust":17.76,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":68,"pop":0.91,"rain":4.65,"uvi":3},{"dt":1621123200,"sunrise":1621106537,"sunset":1621141881,"moonrise":1621120380,"moonset":1621152660,"moon_phase":0.13,"temp":{"day":289.38,"min":285.8,"max":289.38,"night":286.69,"eve":287.2,"morn":286.37},"feels_like":{"day":288.89,"night":285.71,"eve":286.98,"morn":285.71},"pressure":1001,"humidity":70,"dew_point":283.37,"wind_speed":12.74,"wind_deg":331,"wind_gust":22.77,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"clouds":62,"pop":1,"rain":6.24,"uvi":3},{"dt":1621209600,"sunrise":1621192995,"sunset":1621228227,"moonrise":1621209660,"moonset":1621242540,"moon_phase":0.16,"temp":{"day":286.92,"min":283.92,"max":286.96,"night":286.87,"eve":286.63,"morn":284.25},"feels_like":{"day":286.36,"night":282.93,"eve":285.99,"morn":282.93},"pressure":1011,"humidity":77,"dew_point":282.32,"wind_speed":9.95,"wind_deg":317,"wind_gust":18,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":42,"pop":0.4,"rain":0.57,"uvi":3},{"dt":1621296000,"sunrise":1621279453,"sunset":1621314575,"moonrise":1621298580,"moonset":1621332720,"moon_phase":0.19,"temp":{"day":286.23,"min":283.57,"max":287.64,"night":283.57,"eve":284.02,"morn":287.64},"feels_like":{"day":285.84,"night":287.55,"eve":283.38,"morn":287.55},"pressure":1013,"humidity":86,"dew_point":283.22,"wind_speed":10.86,"wind_deg":325,"wind_gust":19.52,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":96,"pop":1,"rain":11.76,"uvi":3}],"alerts":[{"sender_name":"Meteorological Service of New Zealand Limited","event":"wind","start":1620691200,"end":1620727200,"description":"Northwesterly winds may approach severe gale in exposed places at times."}]}
"""

jdata = json.loads(d)
# print(json.dumps(jdata, indent=4, sort_keys=True))

#
# simple access
print( jdata['daily'][0]['pop'] )

#
# there are quite a few 'pop' in the data. find all of them.
# json container can be dict and list.
# traverse data recursive

def traverse_json( jdata, path):
    if isinstance( jdata, dict):
        for k in jdata:
            if k == 'pop':
                path += "['pop']"
                print(path, "=", jdata[k])
            traverse_json( jdata[k], path + "['{k:s}']".format(k=k))
            
    if isinstance( jdata, list):
        for idx, k in enumerate(jdata):
            traverse_json( k, path + "[{idx:d}]".format(idx=idx))

traverse_json( jdata, "")                

User avatar
joelostinspace
Posts: 129
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Openweathermap chance of rain

Tue May 11, 2021 2:18 am

Another way is to use requests, use your own key.

Code: Select all

    def get_dict(self,lat,lon,city,state):
        _lat= f'?lat={lat}'
        _lon= f'&lon={lon}'
        res= requests.get('https://api.openweathermap.org/data/2.5/onecall'+_lat+_lon+'&appid=xxxxxxxxxxhelloxxxxxxxxx&units=imperial') #removed key
        self.one_call= res.json() # creates json file
        .....
I found a json file with all the cities and states to get the longitude and latitude it was on open weather web site

If a little knowledge is dangerous, where is the man who has so much as to be out of danger?


--- Thomas Huxley

Return to “Python”