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 excel e do it again in page 2. But every record is erased when a save it remaining the last one. Sorry for my english, here is my code:
import time
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriveri=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#loop pagination
while(contador < 4):#findind tableelemento = 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. Parse HTML - BeaultifulSoupsoup = BeautifulSoup(html_content, 'html.parser')table = soup.find(name='table')# 3. Data Frame - Pandasdf_full = pd.read_html(str(table))[0]df = df_full[['PLAYER','TEAM', 'PTS']]df.columns = ['jogador','time', 'pontuacao']dados1 = pd.DataFrame(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 exceldados = pd.DataFrame(df)
dados.to_excel("fabinho.xlsx")driver.quit()