El siguiente script está pensado para importar datos desde una Web externa, en formato JSON.
# getting data in JSON format
import requests, json
import pandas as pd
BASE_URL = "https://production.dataviz.cnn.io/index/fearandgreed/graphdata"
START_DATE = '2021-01-01'
r = requests.get("{}/{}".format(BASE_URL, START_DATE))
data = r.json()
fear_greed_index = pd.DataFrame(data['fear_and_greed_historical']['data'])
fear_greed_index['x'] = pd.to_datetime(fear_greed_index['x']//1000, unit='s').dt.strftime('%Y-%m-%d')
fear_greed_index = fear_greed_index.rename(columns={'x':'date','y':'fear_metric'})
fear_greed_index.drop_duplicates()
Me devuelve el error
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
/tmp/ipykernel_6721/1538259400.py in <module>
9
10 r = requests.get("{}/{}".format(BASE_URL, START_DATE))
---> 11 data = r.json()
12
13 fear_greed_index = pd.DataFrame(data['fear_and_greed_historical']['data'])
~/anaconda3/envs/yfinance/lib/python3.9/site-packages/requests/models.py in json(self, **kwargs)
899 if encoding is not None:
900 try:
--> 901 return complexjson.loads(
902 self.content.decode(encoding), **kwargs
903 )
~/anaconda3/envs/yfinance/lib/python3.9/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
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
~/anaconda3/envs/yfinance/lib/python3.9/json/decoder.py in decode(self, s, _w)
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):
~/anaconda3/envs/yfinance/lib/python3.9/json/decoder.py in 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)
¿Cuál es la causa?. Agradeceré ayuda.