0
import yfinance as yahooFinance
import time
import datetime
import pandas as pd

ticker = 'TSLA'

period1 = int(time.mktime(datetime.datetime(2022, 12, 1, 23, 59).timetuple()))  # year,month,date
period2 = int(time.mktime(datetime.datetime(2022, 12, 31, 23, 59).timetuple()))  # year,month,date
interval = '1wk'  # time interval
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
df = pd.read_csv(f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true')
print(df)

The above is my code when I run it , the following error comes

 File "<stdin>", line 1, in <module>
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper      
    return func(*args, **kwargs)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv   
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 575, in _read      
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 933, in __init__   
    self._engine = self._make_engine(f, self.engine)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 1217, in _make_engine
    self.handles = get_handle(  # type: ignore[call-overload]
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 670, in get_handle
    ioargs = _get_filepath_or_buffer(
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 339, in _get_filepath_or_buffer
    with urlopen(req_info) as req:
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 239, in urlopen
    return urllib.request.urlopen(*args, **kwargs)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Users\BRBCO\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

What am I doing wrong?

3
  • Can anyone please tell me where am I going wrong Commented Apr 4, 2022 at 17:16
  • You receive HTTP status code 400 Bad Request which suggests that some of the parameters you pass in your URL are not correct/ correctly formatted etc. . Read the API documentation of the API you' re using to see what you are expected to provide. Commented Apr 4, 2022 at 17:20
  • 1
    Why do you save the url to the variable query_string, and then not use the variable in the next line? (I don't need to know, but you probably want to change that.) Then you can print the value of query_string before you make the request. If it fails, you can try taking what was printed out and putting it into a browser window; that will tell you if it's even a valid site. If it's not, then probably one of your variables is incorrect. Commented Apr 4, 2022 at 17:23

1 Answer 1

2

Both period1 and period2 are invalid, as they lie in the future. Adjust them e. g. with datetime.date(2021,4,5) or datetime.date.today() to get the current date, which is the latest possible date.

import time
import datetime
import pandas as pd

ticker = 'TSLA'

period1 = int(time.mktime(datetime.date(2021,4,5).timetuple()))
period2 = int(time.mktime(datetime.date.today().timetuple())) 
interval = '1wk'

query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'

df = pd.read_csv(query_string)
Sign up to request clarification or add additional context in comments.

1 Comment

Yup Thank you so much I got the correct output now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.