尝试获取代码时出现 ParseError
ParseError while trying to grab tickers
我正在努力让我的程序达到 运行。
我使用的所有软件包都是最新的。
我希望程序打印我选择的股票代码。
但是,我遇到了很多错误,我似乎无法理解我遇到的错误。
我曾尝试将 mktcap_min 和 mktcap_max 更改为更小的值,但无济于事。
我也不确定我的文件和文件夹路径的语法是否正确。
我也查找了人们遇到的类似错误,但我无法将我看到的解决方案实施到我自己的代码中。
我运行命令提示符下的程序得到的错误信息如下:
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
根据我最基本的理解,我可能有记忆问题吗?我怎样才能补救这种情况并让我的程序 运行 没有这些错误?
这是程序的所有代码:
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))
Nasdaq API 已更新,此软件包基于此。见 open issue on github.
解决方案(由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'])
我正在努力让我的程序达到 运行。 我使用的所有软件包都是最新的。
我希望程序打印我选择的股票代码。
但是,我遇到了很多错误,我似乎无法理解我遇到的错误。
我曾尝试将 mktcap_min 和 mktcap_max 更改为更小的值,但无济于事。
我也不确定我的文件和文件夹路径的语法是否正确。
我也查找了人们遇到的类似错误,但我无法将我看到的解决方案实施到我自己的代码中。
我运行命令提示符下的程序得到的错误信息如下:
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
根据我最基本的理解,我可能有记忆问题吗?我怎样才能补救这种情况并让我的程序 运行 没有这些错误?
这是程序的所有代码:
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))
Nasdaq API 已更新,此软件包基于此。见 open issue on github.
解决方案(由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'])