在尝试抓取票子时出现ParseError



我想让我的程序运行。我使用的所有软件包都是最新的。

我希望程序打印我所选择的股票报价。

然而,我得到了很多错误,我似乎不能理解我得到的错误。

我已经尝试改变mktcap_min和mktcap_max较小的值,但无济于事。

我也不确定我的文件&目录路径语法正确

我也查找了类似的错误,但我还没有能够实现解决方案,我已经看到到我自己的代码。

当我在命令提示符中运行程序时,我得到的错误消息如下:

Traceback (most recent call last):
File "C:UsersAnthonyDesktopClassestestProgramspoker.py", line 51, in <module>
tickers = gt.get_tickers_filtered(mktcap_min = 150000, mktcap_max = 10000000, sectors = None)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagesget_all_tickersget_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:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagesget_all_tickersget_tickers.py", line 145, in __exchange2list_filtered
df = __exchange2df(exchange)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagesget_all_tickersget_tickers.py", line 134, in __exchange2df
df = pd.read_csv(data, sep=",")
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagespandasutil_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 586, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 488, in _read
return parser.read(nrows)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 1047, in read
index, columns, col_dict = self._engine.read(nrows)
File "C:UsersAnthonyAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersc_parser_wrapper.py", line 223, in read
chunks = self._reader.read_low_memory(nrows)
File "pandas_libsparsers.pyx", line 801, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas_libsparsers.pyx", line 857, in pandas._libs.parsers.TextReader._read_rows
File "pandas_libsparsers.pyx", line 843, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas_libsparsers.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:UsersAnthonyDesktopClassestestProgramspokerStorage")
os.mkdir(r"C:UsersAnthonyDesktopClassestestProgramspokerStorage")
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:UsersAnthonyDesktopClassestestProgramspokerStoragehistoricalData.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))

纳斯达克API更新了,这个包是基于它的。参见github上的开放问题。

解决方案(github用户possum提供)

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'])

相关内容

  • 没有找到相关文章

最新更新