Home > Back-end >  ParseError while trying to grab tickers
ParseError while trying to grab tickers

Time:10-27

I am trying to get my program to run. All the packages I am using are up to date.

I am expecting the program to print stock tickers that I have chosen.

However, I am getting a lot of errors I can't seem to understand the errors I am getting.

I have tried changing mktcap_min and mktcap_max to smaller values but to no avail.

I'm also not sure if my file & folder paths are correct syntax.

I have also looked up similar errors people were having but I haven't been able to implement the solutions I've seen into my own code.

The error messages I get when I run the program in Command Prompt are as follows:

Traceback (most recent call last):
  File "C:\Users\Anthony\Desktop\Classes\testPrograms\poker.py", line 51, in <module>
    tickers = gt.get_tickers_filtered(mktcap_min = 150000, mktcap_max = 10000000, sectors = None)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\get_all_tickers\get_tickers.py", line 84, in get_tickers_filtered
    tickers_list.extend(__exchange2list_filtered(exchange, mktcap_min=mktcap_min, mktcap_max=mktcap_max, sectors=sectors))
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\get_all_tickers\get_tickers.py", line 145, in __exchange2list_filtered
    df = __exchange2df(exchange)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\get_all_tickers\get_tickers.py", line 134, in __exchange2df
    df = pd.read_csv(data, sep=",")
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 586, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 488, in _read
    return parser.read(nrows)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\readers.py", line 1047, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\Anthony\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 223, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 801, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 857, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 843, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1925, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 5, saw 47

From my very basic level of understanding, could I possibly have a memory problem? How can I remedy this situation and get my program to run without these errors?

This is the all the code for the program:


import yfinance as yf, pandas as pd, shutil, os
from get_all_tickers import get_tickers as gt

tickers = gt.get_tickers_filtered(mktcap_min = 150000, mktcap_max = 10000000, sectors = None)
print("the amount of stocks chosen to observe: "   str(len(tickers)))

shutil.rmtree(r"C:\Users\Anthony\Desktop\Classes\testPrograms\pokerStorage")
os.mkdir(r"C:\Users\Anthony\Desktop\Classes\testPrograms\pokerStorage")

Stock_Failure = 0
Stocks_Not_Imported = 0

i=0
while (i < len(tickers)) and (Amount_of_API_Calls < 1800):
    try:
        stock = tickers[i]
        temp = yf.Ticker(str(stock))
        Hist_data = temp.history(period="max")
        Hist_data.to_csv(r"C:\Users\Anthony\Desktop\Classes\testPrograms\pokerStorage\historicalData.csv")
        time.sleep(2)
        Amount_of_API_Calls  = 1
        Stock_Failure = 0
        i  = 1

    except ValueError:
        print("Yahoo Finance Back-end Error, Attempting to Fix")

        if Stock_Failure > 5:
            i =1
            Stocks_Not_Imported  = 1
            Amount_of_API_Calls  = 1
            Stock_Failure  = 1
            
print("The amount of stocks successfully imported: "   str(i - Stocks_Not_Imported))

CodePudding user response:

The Nasdaq API got updated, which this package is based on. See the open issue on github.

The solution (provided by github user Possums)

import requests
import pandas as pd

headers = {
    'authority': 'api.nasdaq.com',
    'accept': 'application/json, text/plain, */*',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
    'origin': 'https://www.nasdaq.com',
    'sec-fetch-site': 'same-site',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://www.nasdaq.com/',
    'accept-language': 'en-US,en;q=0.9',
}

params = (
    ('tableonly', 'true'),
    ('limit', '25'),
    ('offset', '0'),
    ('download', 'true'),
)

r = requests.get('https://api.nasdaq.com/api/screener/stocks', headers=headers, params=params)
data = r.json()['data']
df = pd.DataFrame(data['rows'], columns=data['headers'])
  • Related