Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python data scraping but i need it to write next coloum in excel instead of replacing as it is doing now

dylanshrpe

New Coder
i need help with my data scraper it gets all the data and writes it to an excel sheet , but whenever i want to run it again it replaces the old data and id like it to be wrote a few coloums to the right instead without replacing all data can anyone help with this please?(i run this in pycharm)

Python:
import bs4
import requests
import csv
import pandas as pd
from openpyxl.workbook import Workbook
from selenium import webdriver
wb = Workbook()
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)
driver = webdriver.Chrome(executable_path=r'C:\Users\Dylan\PycharmProjects\pythonProject9\chromedriver.exe')#C:\Users\Dylan\Desktop

site=input("Enter site :")
players_filtered=[]
players_final=[]
players = []
driver.get(site)
lis=driver.find_elements_by_class_name('page-item ')
pages=len(lis)+1
print(pages)
#googling the error :P


def scrape_names():
    global players
    global players_filtered
    global players_final
    global pages
    global player_count


    atts=[]
    x = int(site.index('page='))

    s1 = site[x + 6:]
    s2 = site[:x + 5]

    for page in range(pages):

        response = requests.get((s2 + str(page) + s1))
        soup = bs4.BeautifulSoup(response.text, 'html.parser')
        p_name=soup.findAll('a')
        for i in p_name:
            atts.append(str(i.get('href')))

    for att in atts:
        if ('21/player/'in att and att not in players):
            players.append(att)

    for player in players :
        if player not  in players_filtered:
            players_filtered.append(player)

    scrape_player()

    with open('futbin..csv', 'w', newline='') as file:
        writer = csv.writer(file)
        headers = ['Name','INFO','PS','X_ONE']
        writer.writerow(headers)
        for data in players_final:
            writer.writerow(data)

    df_new = pd.read_csv('futbin.csv')
    GFG = pd.ExcelWriter('PlayerResult.xlsx')
    df_new.to_excel(GFG, index=False)
    print('Task Completed!')
    GFG.save()




def scrape_player():
    global players
    global players_filtered
    global players_final
    for player in players_filtered:

        driver.get("https://www.futbin.com"+str(player))
        driver.implicitly_wait(10)
        xone=driver.find_element_by_xpath('//*[@id="ps-lowest-1"]').text
        ps=driver.find_element_by_xpath('//*[@id="xbox-lowest-1"]').text


        response2 = requests.get("https://www.futbin.com"+str(player))
        soup = bs4.BeautifulSoup(response2.text, 'html.parser')
        nm = soup.find('h1')
        name=""
        info=""

        if '-' in nm.text:
            na=nm.text.split('-')
            name=na[0].strip()
            info=na[1].strip()

        else:
            na=nm.text.strip()
            info="."


        players_final.append([name,info,xone,ps])


try:
    scrape_names()
except Exception as e :
    driver.quit()

driver.quit()
 
Back
Top Bottom