I'm following this video that uses Python, and I'm using Jupyter Notebook for the 1st project for building an equal-weight S&P 500 index fund. At around 56:30 we're starting to pull data for all the stocks into a Pandas DataFrame object.
The loop for appending all the info is returning JSONDecodeError: Expecting value: line 1 column 1 (char 0). I would also possibly like an alternate way to write this code without append, since it will be deprecated in the future. We're using a sandbox API from IEX Cloud if that helps at all.
The code works when putting [:5] after 'Ticker' and returns the first 5 stock information, so I don't understand why including all the stocks gives a different response.
The code:
stocks = pd.read_csv('sp_500_stocks.csv')
from secrets import IEX_CLOUD_API_TOKEN
my_columns = ['Ticker', 'Stock Price', 'Market Capitalization', 'Number of Shares to Buy']
final_dataframe = pd.DataFrame(columns = my_columns)
for symbol in stocks['Ticker']:
api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
data = requests.get(api_url).json()
final_dataframe = final_dataframe.append(
pd.Series(
[
symbol,
data['latestPrice'],
data['marketCap'],
'N/A'
],
index = my_columns
),
ignore_index = True
)
The error:
JSONDecodeError Traceback (most recent call last)
Input In [73], in <cell line: 2>()
2 for stock in stocks['Ticker']:
3 api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
----> 4 data = requests.get(api_url).json()
5 final_dataframe = final_dataframe.append(
6 pd.Series(
7 [
(...)
15 ignore_index = True
16 )
File ~\anaconda3\lib\site-packages\requests\models.py:899, in Response.json(self, **kwargs)
897 if encoding is not None:
898 try:
--> 899 return complexjson.loads(
900 self.content.decode(encoding), **kwargs
901 )
902 except UnicodeDecodeError:
903 # Wrong UTF codec detected; usually because it's not UTF-8
904 # but some other 8-bit codec. This is an RFC violation,
905 # and the server didn't bother to tell us what codec *was*
906 # used.
907 pass
File ~\anaconda3\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
341 s = s.decode(detect_encoding(s), 'surrogatepass')
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
348 cls = JSONDecoder
File ~\anaconda3\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
332 def decode(self, s, _w=WHITESPACE.match):
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
File ~\anaconda3\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
You can achieve the same result checking the data requested is present.
final_dataframe = pd.DataFrame(columns = my_columns)
lst = []
for stock in stocks['Ticker']:
api_url = f'https://sandbox.iexapis.com/stable/stock/{stock}/quote/?token={IEX_CLOUD_API_TOKEN}'
api_url = api_url.rstrip('\n')
r = requests.get(api_url)
if r:
data = r.json()
lst.append([stock, data['latestPrice'], data['marketCap'], 'N/A'])
final_dataframe = pd.DataFrame(lst, columns=my_columns)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.