【发布时间】:2021-02-03 23:02:44
【问题描述】:
我有一个从 Finviz 抓取数据的函数,该函数的一部分编译了一个指标列表。如果网站地址在遍历股票列表时不存在,它仍会在 Dataframe 中创建一行,其中包含股票名称,但不包含任何指标。我想要这样,如果它找不到该网站,该行要么在循环结束时被删除,要么根本不包括在内。任何有关我如何调整此功能的见解将不胜感激。
def get_fundamental_data(df):
for symbol in df.index:
try:
#url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())
r = requests.get("http://finviz.com/quote.ashx?t="+ symbol.lower(),headers=headers)
soup = bs(r.content,'html.parser')
for m in df.columns:
output = fundamental_metric(soup,m)
df.loc[symbol,m] = output
df = df.replace(['-'], np.NaN)
except Exception as e:
print (symbol, 'Not Found')
print(e)
return df
【问题讨论】:
标签: python pandas function dataframe exception