Python - excel 导出 [英] Python - excel export

查看:44
本文介绍了Python - excel 导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("https://www.bankier.pl/gielda/notowania/akcje")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
writer = pd.ExcelWriter('test.xlsx',engine='xlsxwriter')  
workbook = writer.book
df.to_excel(writer,sheet_name='quotations',startrow=0 , startcol=0)

运行后,出现如下错误:

after running it, the following error shows up:

AttributeError: 'list' object has no attribute 'to_excel'

有人可以帮我吗?

推荐答案

来自 pandas.read_html 文档: 将 HTML 表读入 DataFrame 对象列表.

所以,df 是一个数据帧列表.如果您希望只有一个,那么使用 df[0].to_excel...

So, df is a list of dataframes. If you expect to only have one, it may be safe for you to use df[0].to_excel...

这篇关于Python - excel 导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆