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

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...