0

I am trying to download data from the iex api using python and currently I am to the point where i get the data, but now i want to format it.

basically i get a lot of data which i do not care about, I just want to have the "float" section.

The data should look like this:

Ticker, Float,

AAPL, 4700000000, (something like that)

The code I am using:

import requests 

url = "https://api.iextrading.com/1.0/stock/aapl/stats" 

response = requests.get(url).json()

print (response)

I would be verry happy if someone could explain me how to do this.

Kind regards

Right now I have the code:

import requests 

url = "https://api.iextrading.com/1.0/stock/aapl/stats" 

response = requests.get(url).json()

data = (response['symbol'], response['float'])

import json filename='resp.json'

with open(filename, 'a+') as outfile:
    json.dump(data, outfile, indent=4)

import requests 

url = "https://api.iextrading.com/1.0/stock/tsla/stats" 

response = requests.get(url).json()

data = (response['symbol'], response['float'])

import json filename='resp.json'

with open(filename, 'a+') as outfile:
    json.dump(data, outfile, indent=4)

I would like the data to show as:

Ticker, Float,

AAPL, 4700000000,

TSLA, 1700000000,

(Ticker and float do not neceserally have to be placed above, i could do that myself in excel power query anyway).

Serenity
  • 35,289
  • 20
  • 120
  • 115
Hoogoo
  • 15
  • 1
  • 8

2 Answers2

1

You can just treat it like a dictionary. response['float'] would give you the float. Similarly for any key.

import requests
url = "https://api.iextrading.com/1.0/stock/aapl/stats"
response = requests.get(url).json()
print (response['float'])
print(response['symbol'])

Output

4705473314
AAPL
Bitto
  • 7,937
  • 1
  • 16
  • 38
1

Your code is doing exactly what it should do, if you want a certain part of the json, just access it.

import requests 

url = "https://api.iextrading.com/1.0/stock/aapl/stats" 

response = requests.get(url).json()

print(response['float'])
>4705473314
print(response['symbol'])
>'AAPL'

print(response['symbol'], response['float'])

to store response in a json file, we can do something like this

import json
filename='resp.json'

with open(filename, 'w') as outfile:
    json.dump(response, outfile, indent=4)
SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • 1 more question, If i would like to save the data to a text file, how can i achieve this? Currently I am using saveFile = open('Float Data.txt', 'a+') saveFile.write(text_lines) saveFile.close() – Hoogoo Feb 14 '19 at 20:02
  • @Hoogoo why not save as JSON file? – SuperStew Feb 14 '19 at 20:04
  • i added an example of that – SuperStew Feb 14 '19 at 20:17
  • Thanks for your efforts, Unfortunately i still have a small issue, when i type in your code i get the response: File "main.py", line 10 with open(filename, 'w') as outfile: ^ SyntaxError: invalid syntax I will update my origonal post to show the code I am using now – Hoogoo Feb 14 '19 at 20:23
  • Alright, so i fixed that issue. Now, I have another one :p – Hoogoo Feb 14 '19 at 20:48
  • thats progress, what is it? – SuperStew Feb 14 '19 at 20:54
  • So basically, when I copy this down and change the ticker symbol (from $aapl to $tsla, for example), and change 'w' to 'a+', it will write multiple outputs to the same file. the problem is that the data is shown below eachother, and i would like to show the data as: Ticker, Float, AAPL, 470000000 TSLA, 170000000 Is there a way i could use this to download multiple stocks at the same time, or if i could parse it to a csv where i get something like i mentioned above? – Hoogoo Feb 14 '19 at 20:59