I am trying this simple code to read data from a website, but it gives me KeyError['p']
:
for i in range(25200):
time.sleep(1)
with requests.Session() as s:
data = {'current' : 'afghan_usd' }
r = s.get('http://call5.tgju.org/ajax.json?2019061716-20190617171520-I4OJ3OcWf4gtpzr3JNC5', json = data ).json()
#print(r)
for key, value in r["current"].items():
last_prices = (r[key]['p'])
z.append(last_prices)
mid.append(mean(z))
The given r
is like this:
{'current': {'afghan_usd': {'p': '154530', 'h': '157260', 'l':
'154530', 'd': '3640', 'dp': 2.36, 'dt': 'low', 't': '۱۷:۲۷:۰۳',
't_en': '17:27:03', 't-g': '۱۷:۲۷:۰۳', 'ts': '2019-06-17 17:27:03'}}
And you can see the full content of response(r
) here: https://github.com/rezaee/coursera-test/issues/1
EDIT:
I edited my code like this:
for i in range(25200):
time.sleep(1)
with requests.Session() as s:
data = {'current' : 'afghan_usd' }#code}
r = s.get('http://call5.tgju.org/ajax.json?2019061716-20190617171520-I4OJ3OcWf4gtpzr3JNC5', json = data ).json()
#print(r)
for key, value in r["current"]["afghan_usd"].items():
last_prices = float(value.replace("," , ""))
z.append(last_prices)
mid.append(mean(z))
But I get this new error:
AttributeError: 'float' object has no attribute 'replace'