如何在使用Python保存到excel时分离pd.read_html()中的多个dataframe

我试图保存通过pd.read_html()引入的多个表中的数据。 如果我打印df ,我可以看到它捕获所有的数据,但是当保存数据时,只保存第一个表格到Excel。 我怎么把表格分开,这样我就可以把每一个表格保存到excel的单独表格中(例如,表格1中的季度损益表,表格2中的年度损益表等)。 以下是我的代码。 任何帮助表示赞赏。

 dfs = pd.read_html(https://www.google.com/finance?q=googl&fstype=ii, flavor='html5lib') writer = pd.ExcelWriter(output.xlsx, engine='xlsxwriter') for df in dfs: df.to_excel(writer, sheet_name='Sheet1') writer.save() 

您可以迭代您的列表并将其刷新到同一工作簿的新工作表

 import pandas as pd dfs = pd.read_html('https://www.google.com/finance?q=googl&fstype=ii', flavor='html5lib') # Create a Pandas Excel writer. xlWriter = pd.ExcelWriter('myworkbook.xlsx', engine='xlsxwriter') # Write each df to its own sheet for i, df in enumerate(dfs): df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i)) # Close the writer and output the Excel file (mandatory!) xlWriter.save()