使用Marioriostoev的Finviz API出现解析错误求助
Finviz API解析错误排查:
Unable to parse page for ticker: None 我使用Marioriostoev开发的Finviz API获取股票数据时,出现了解析错误,提示Unable to parse page for ticker: None。程序原本运行正常,功能是通过设置筛选条件匹配符合要求的股票,将结果导出至Excel文件,并将股票图表保存至指定文件夹,但现在无法正常运行,不清楚错误出在哪个模块。相关代码如下:
#!/usr/bin/python3 !pip install finviz~=1.4.6 from finviz.screener import Screener import os import datetime Current_Date = datetime.datetime.today().strftime ('%d-%b-%Y') # Input valperiod = input('Enter Type of Graphic d,w,m: ') if valperiod == "w" or "m": valta="0" else: valta = "1" # Create the Folder where the data and charts are going to be stored newpath = 'D:\\Users\\Jose Mizrahi\\Documents\\finviz-master\\finviz-master\\'+'Breakout1 '+str(Current_Date)+'\\' if not os.path.exists(newpath): os.makedirs(newpath) # Get dict of available filters # filters dict contains the corresponding filter tags filters = Screener.load_filter_dict() filters = [ "cap_small", "fa_sales5years_pos", "sh_curvol_o200", "sh_relvol_o1", "ta_highlow52w_b0to10h", "ty=c&p=w&tas=0", "ta_volatility_mo3&ft=4&o=ticker" ] stock_list = Screener(filters=filters, table="Performance") print(stock_list) # Monthly, Candles, Large, No Technical Analysis if valperiod == "d": stock_list.get_charts(period="d", chart_type='c', size='l', ta="1") else: stock_list.get_charts(period=valperiod, chart_type='c', size='l', ta="0") # period='d' > daily # period='w' > weekly # period='m' > monthly # chart_type='c' > candle # chart_type='l' > lines # size='m' > small # size='l' > large # ta='1' > display technical analysis # ta='0' > ignore technical analysis print("Retrieving stock data...") stock_data = stock_list.get_ticker_details() print(stock_data) os.chdir(newpath) # Export the screener results to CSV file stock_list.to_csv("JosyBreakout1.csv") # Create a SQLite database # stock_list.to_sqlite("sp500.sqlite")
错误原因及修复方案
1. 筛选器参数格式错误
手动定义的filters列表中混入了非标准筛选器标签:"ty=c&p=w&tas=0"和"ta_volatility_mo3&ft=4&o=ticker"。这些是Finviz页面的URL参数,不属于筛选器标签范畴,需从filters中移除,相关配置通过Screener初始化的其他参数传递,或使用load_filter_dict()中的合法标签。
修正后的筛选器列表:
filters = [ "cap_small", "fa_sales5years_pos", "sh_curvol_o200", "sh_relvol_o1", "ta_highlow52w_b0to10h", "ta_volatility_mo3" # 仅保留合法筛选器标签 ]
排序参数o=ticker在初始化Screener时通过order参数传递:
stock_list = Screener(filters=filters, table="Performance", order="ticker")
2. 条件判断逻辑错误
if valperiod == "w" or "m": 逻辑有误,Python中该表达式永远为True(因为"m"本身是真值)。正确写法:
if valperiod == "w" or valperiod == "m": valta = "0" else: valta = "1"
3. 图表保存路径缺失
调用get_charts时需指定保存路径到创建的newpath,通过path参数传递:
if valperiod == "d": stock_list.get_charts(period="d", chart_type='c', size='l', ta="1", path=newpath) else: stock_list.get_charts(period=valperiod, chart_type='c', size='l', ta="0", path=newpath)
4. 冗余代码清理
删除重复的from finviz.screener import Screener导入语句。
修正后的完整代码
#!/usr/bin/python3 !pip install finviz~=1.4.6 from finviz.screener import Screener import os import datetime Current_Date = datetime.datetime.today().strftime ('%d-%b-%Y') # Input valperiod = input('Enter Type of Graphic d,w,m: ') if valperiod == "w" or valperiod == "m": valta = "0" else: valta = "1" # Create the Folder where the data and charts are going to be stored newpath = 'D:\\Users\\Jose Mizrahi\\Documents\\finviz-master\\finviz-master\\'+'Breakout1 '+str(Current_Date)+'\\' if not os.path.exists(newpath): os.makedirs(newpath) # Get dict of available filters filters = [ "cap_small", "fa_sales5years_pos", "sh_curvol_o200", "sh_relvol_o1", "ta_highlow52w_b0to10h", "ta_volatility_mo3" ] # Initialize screener with correct filters and order stock_list = Screener(filters=filters, table="Performance", order="ticker") print(stock_list) # Monthly, Candles, Large, No Technical Analysis if valperiod == "d": stock_list.get_charts(period="d", chart_type='c', size='l', ta="1", path=newpath) else: stock_list.get_charts(period=valperiod, chart_type='c', size='l', ta="0", path=newpath) print("Retrieving stock data...") stock_data = stock_list.get_ticker_details() print(stock_data) os.chdir(newpath) # Export the screener results to CSV file stock_list.to_csv("JosyBreakout1.csv")
内容的提问来源于stack exchange,提问作者Josy
相关产品推荐
相关产品推荐

