Skip to content Skip to sidebar Skip to footer

Exporting Dataframe To Excel Using Pandas Without Subscribe

How can I export DataFrame to excel without subscribe? For exemple: I'm doing webscraping and there is a table with pagination, so I take the page 1 save it in DataFrame, export to

Solution 1:

You are re-assigning df to whatever data you retrieved everytime you go through the loop. A solution would be to append the data to a list and then pd.concat the list at the end.

import time
import pandas as pd
from bs4 importBeautifulSoupfrom selenium import webdriver


i=1
url = "https://stats.nba.com/players/traditional/?PerMode=Totals&Season=2019-20&SeasonType=Regular%20Season&sort=PLAYER_NAME&dir=-1"

driver = webdriver.Firefox(executable_path=r'C:/Users/Fabio\Desktop/robo/geckodriver.exe')

driver.get(url)
time.sleep(5)


driver.find_element_by_xpath("/html/body/main/div[2]/div/div[2]/div/div/nba-stat-table/div[2]/div[1]/table/thead/tr/th[9]").click()



contador = 1
df_list = list()
#loop pagination
while(contador < 4):

    #findind table
    elemento = driver.find_element_by_xpath("/html/body/main/div[2]/div/div[2]/div/div/nba-stat-table/div[2]")
    html_content = elemento.get_attribute('outerHTML')

    # 2.ParseHTML - BeaultifulSoup
    soup = BeautifulSoup(html_content, 'html.parser')
    table = soup.find(name='table')

    # 3.DataFrame - Pandas
    df_full = pd.read_html(str(table))[0]
    df = df_full[['PLAYER','TEAM', 'PTS']]
    df.columns = ['jogador','time', 'pontuacao']
    df_list.append(df)
    
    driver.find_element_by_xpath("/html/body/main/div[2]/div/div[2]/div/div/nba-stat-table/div[1]/div/div/a[2]").click()

    contador = contador + 1

#4.export to excel

dados = pd.concat(df_list)
dados.to_excel("fabinho.xlsx")

driver.quit()

Post a Comment for "Exporting Dataframe To Excel Using Pandas Without Subscribe"