Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pandas - Python - excel export

I've got this code:

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'

Can anybody help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

pd.read_html returns a list of dataframes, you need to use an indexer to get the first dataframe.

Use:

df = pd.read_html(str(table))[0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...