ParserError:标记数据时出错 C 错误:第 23 行中应有 1 个字段

问题描述

我想编制一份符合我设定标准的股票清单。我正在寻找市值在 150,000 美元到 10,000,000 美元(以百万计)之间的公司。运行下面的代码时出现错误。我想知道我到底做错了什么

''' 将 yfinance 导入为 yf,将熊猫导入为 pd、shutil、os、time、glob、smtplib、ssl 从 get_all_tickers 导入 get_tickers 作为 gt

tickers = gt.get_tickers_filtered(mktcap_min=150000,mktcap_max=10000000)

print("选择观察的股票数量:" + str(len(tickers)))

'''

附加数据:

runfile('A:/Misc Financial Docs/stocks_data/OBV_Email.py',wdir='A:/Misc Financial Docs/stocks_data') 回溯(最近一次调用):

文件“A:\Misc Financial Docs\stocks_data\OBV_Email.py”,第 13 行,在 股票代码 = gt.get_tickers_filtered(mktcap_min=150000,mktcap_max=10000000)

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\get_all_tickers\get_tickers.py”,第 84 行,在 get_tickers_filtered tickers_list.extend(__exchange2list_filtered(exchange,mktcap_min=mktcap_min,mktcap_max=mktcap_max,sectors=sectors))

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\get_all_tickers\get_tickers.py”,第 145 行,在 __exchange2list_filtered df = __exchange2df(exchange)

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\get_all_tickers\get_tickers.py”,第 134 行,在 __exchange2df 中 df = pd.read_csv(data,sep=",")

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\pandas\io\parsers.py”,第605行,在read_csv中 返回 _read(filepath_or_buffer,kwds)

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\pandas\io\parsers.py”,第463行,_read 返回 parser.read(nrows)

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\pandas\io\parsers.py”,第1052行,读取 索引,列,col_dict = self._engine.read(nrows)

文件“c:\users\aos82\appdata\local\programs\python\python39\lib\site-packages\pandas\io\parsers.py”,第2056行,读取 数据 = self._reader.read(nrows)

文件“pandas_libs\parsers.pyx”,第 756 行,在 pandas._libs.parsers.TextReader.read 中

文件“pandas_libs\parsers.pyx”,第 771 行,在 pandas._libs.parsers.TextReader._read_low_memory 中

文件“pandas_libs\parsers.pyx”,第 827 行,在 pandas._libs.parsers.TextReader._read_rows 中

文件“pandas_libs\parsers.pyx”,第 814 行,在 pandas._libs.parsers.TextReader._tokenize_rows 中

文件“pandas_libs\parsers.pyx”,第 1951 行,在 pandas._libs.parsers.raise_parser_error 中

ParserError:标记数据时出错。 C 错误:第 23 行应为 1 个字段,看到 4

解决方法

这似乎与纳斯达克 API 的新网址有关。作者在此处打开了一张票:https://github.com/shilewenuw/get_all_tickers/issues/12。基本上他们使用了一个新的标题。

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

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

同样在上面的链接中,有一个贡献者可以完全替代我使用的 get_tickers.py 文件,它对我有用。