我遵循了一個教程,但我不喜歡結果,所以我試圖優化它,但我似乎無法找到一種方法來始終制作新的數據框。我知道這是 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)
總是創建一個新的 DataFrame
幕布斯6054654
2023-07-18 15:28:16