我遵循了一个教程,但我不喜欢结果,所以我试图优化它,但我似乎无法找到一种方法来始终制作新的数据框。我知道这是 while 循环的结果。所以我想要的是附加到我制作的数据框的价格。 提前致谢!import pandas as pdimport bs4import requestsfrom bs4 import BeautifulSoupimport datetime#getting actual pricedef Real_time_Price(stock): url = ('https://finance.yahoo.com/quote/'+stock+'?p='+stock) r=requests.get(url) web_content=BeautifulSoup(r.text, 'lxml') web_content = web_content.find('div',{'class':"My(6px) Pos(r) smartphone_Mt(6px)"}) web_content = web_content.find('span').text return web_content这就是我的问题开始的地方while True: price = [] col = [] time_stamp = datetime.datetime.now() #de milli seconden wegknallen. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S") #welke stocks wilje checken ticker_symbols = ['TSLA','AAPL','MSFT'] for stock in ticker_symbols: price.append(Real_time_Price(stock)) #getting it into a pandas dataframe #You want [data] for pandas to understand they're rows. df=pd.DataFrame(data=[price], index=[time_stamp], columns=ticker_symbols) print(df)
1 回答
回首忆惘然
TA贡献1847条经验 获得超11个赞
建一次数据框并使用 DataFrame.loc([]) 追加
df=pd.DataFrame(index=[time_stamp], columns=ticker_symbols)
while True:
price = []
col = []
time_stamp = datetime.datetime.now()
#de milli seconden wegknallen.
time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
#welke stocks wilje checken
ticker_symbols = ['TSLA','AAPL','MSFT']
for stock in ticker_symbols:
price.append(Real_time_Price(stock))
df.loc[time_stamp]=price
添加回答
举报
0/150
提交
取消