Reputation: 405
Ok, so what I need is just CVS file with basic stocks data with additional sector column for each company. Getting sectors and tickers from wikipedia works fine.
But Google/Yahoo data doesn't want to work anymore, so I've tried to use Quandl.I've recently made a few technical indicators based on it so I thought it would be great.But there is following error.
I am still in doubt if I can format my request like that so I assume that there is a problem but I can't figure a way to make it work.
Thank you for any advice and sorry for misspelings.
from bs4 import BeautifulSoup
import requests
import pandas as pd
import lxml
import quandl as qdl
def get_ticker_and_sector(url='https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'):
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, 'lxml')
table = soup.find('table')
sp500 = {}
for tr in table.find_all('tr')[1:]:
tds = tr.find_all('td')
ticker = tds[0].text
sector = tds[3].text
sp500[ticker] = sector
return sp500
if __name__ == '__main__':
sp500 = get_ticker_and_sector()
for i, (ticker, sector) in enumerate(sp500.items()):
stock_df = qdl.get('WIKI/%s', start_date="2010-12-11", end_date="2011-12-31")%(ticker)
stock_df['Name'] = ticker
stock_df['Sector'] = sector
if i == 0:
all_df = stock_df
else:
all_df = all_df.append(stock_df)
all_df.to_csv('all_sp500_data_2.csv')
Error.
Traceback (most recent call last):
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/connection.py", line 55, in parse
return response.json()
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/requests/models.py", line 886, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tomek/PycharmProjects/untitled4/get_file.py", line 30, in <module>
stock_df = qdl.get('WIKI/%s', start_date="2010-12-11", end_date="2011-12-31")%('ticker')
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/get.py", line 48, in get
data = Dataset(dataset_args['code']).data(params=kwargs, handle_column_not_found=True)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/model/dataset.py", line 47, in data
return Data.all(**updated_options)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/operations/list.py", line 14, in all
r = Connection.request('get', path, **options)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/connection.py", line 36, in request
return cls.execute_request(http_verb, abs_url, **options)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/connection.py", line 44, in execute_request
cls.handle_api_error(response)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/connection.py", line 61, in handle_api_error
error_body = cls.parse(resp)
File "/home/tomek/tomek-workspace/pythons/udemy/lib/python3.5/site-packages/quandl/connection.py", line 57, in parse
raise QuandlError(http_status=response.status_code, http_body=response.text)
quandl.errors.quandl_error.QuandlError: (Status 400) Something went wrong. Please try again. If you continue to have problems, please contact us at [email protected].
Upvotes: 1
Views: 283
Reputation: 23
The traceback is giving you an 400 status code which means something is wrong with the request to the API.
More specifically, Quandl's documentation says the following regarding a 400 status code:
We could not recognize your API key. Please check your API key and try again. You can find your API key under your account settings.
As a good starting point, have you entered your API key after installing the Quandl module? Steps are here for more information on how to do it.
Please note, I only glanced at the rest of your code, and didn't test it out fully to see if there are other errors!
Upvotes: 0