Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

def function10(): alldatadf = pd.DataFrame() for domain, df in zip(basedomains, dflist):…

  1. User

    def function10(): all_data_df = pd.DataFrame()

    for domain, df in zip(base_domains, df_list):
        # List of URLs to scrape
        urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
        for idx, url in enumerate(urls):
            all_data = []
    
            driver.get(url)
            div_content_driver = driver.find_element(By.ID, "prepared_content")
    
            select_element = div_content_driver.find_element(By.XPATH, ".//select")
            select = Select(select_element)
    
            while select.first_selected_option.get_attribute('value') != "-1":
                select.select_by_value("-1")
    
            html_content = driver.page_source
            soup = BeautifulSoup(html_content, "html.parser")
    
            div_content = soup.find("div", {"id": "prepared_content"})
            table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None # find table in each page
    
            if table:
                data = []
                prev_row = None
                first_row = True
    
                budget_div = soup.find("div", class_="head")
                if budget_div:
                    h5_element = budget_div.find("h5")
                    if h5_element:
                        klient_name = h5_element.text.split(" - Budżet")[0].strip()
                    else:
                        klient_name = "Unknown Client"
                else:
                    klient_name = "Unknown Client"
    
                for tr in table.find_all("tr"):
                    if first_row:
                        first_row = False
                        continue 
    
                    tds = tr.find_all("td")   
    
                    if len(tds) >= 6:
                        row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                        row_data_without_type = [row_data[0], row_data[-2], row_data[-1]]
    
                        if all(row_data):
                            if prev_row is None or row_data_without_type != prev_row_data_without_type:
                                all_data.append(row_data)
                                prev_row = row_data
                                prev_row_data_without_type = row_data_without_type
                            else:
                                print(f"Same row as previous, not appended: {row_data}")
                        else:
                            print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    
                all_data.extend(data)
                all_data_with_klient = [[klient_name] + row for row in all_data]
                df_data = pd.DataFrame(all_data_with_klient, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca"])
                all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
    # Filter the DataFrame to separate rows with status "nowe".
    filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
    # Format emails before grouping
    filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
    # Group the DataFrame by 'Wykonawca' (email address)
    grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
    # Save the filtered DataFrame to a separate CSV file.
    # if you want use groupby usage is: .groupby(['Wykonawca', 'Klient']).sum().reset_index() or to_csv dosent work
    filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index().to_csv("filtered_nowe_data.csv", index=False)
    print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
    # Now, filter out rows with None 'Wykonawca' before creating messages
    filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    

    i want add couting rows in this function, i want to df_data Data frame add next column which will count how much rows are merged with skipped ones so i can know how much texts were published i also want to add this count number in filtered_df_nowe

  2. Assistant

    Certainly! You can achieve this by maintaining a separate counter that counts how many texts have been skipped (not appended). Then you can add that count as a new column to the df_data DataFrame and then propagate it to filtered_df_nowe.

    Here's the modified version of your function with the added counting logic incorporated:

    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.select import Select
    from bs4 import BeautifulSoup
    
    def format_email(email):
        # Assuming format_email function is defined elsewhere
        return email.strip().lower()
    
    def function10():
        all_data_df = pd.DataFrame()
    
        for domain, df in zip(base_domains, df_list):
            # List of URLs to scrape
            urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
            for idx, url in enumerate(urls):
                all_data = []
    
                driver.get(url)
                div_content_driver = driver.find_element(By.ID, "prepared_content")
    
                select_element = div_content_driver.find_element(By.XPATH, ".//select")
                select = Select(select_element)
    
                while select.first_selected_option.get_attribute('value') != "-1":
                    select.select_by_value("-1")
    
                html_content = driver.page_source
                soup = BeautifulSoup(html_content, "html.parser")
    
                div_content = soup.find("div", {"id": "prepared_content"})
                table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None # find table in each page
    
                if table:
                    data = []
                    prev_row = None
                    prev_row_data_without_type = None
                    first_row = True
                    skipped_count = 0
    
                    budget_div = soup.find("div", class_="head")
                    if budget_div:
                        h5_element = budget_div.find("h5")
                        if h5_element:
                            klient_name = h5_element.text.split(" - Budżet")[0].strip()
                        else:
                            klient_name = "Unknown Client"
                    else:
                        klient_name = "Unknown Client"
    
                    for tr in table.find_all("tr"):
                        if first_row:
                            first_row = False
                            continue 
    
                        tds = tr.find_all("td")   
    
                        if len(tds) >= 6:
                            row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                            row_data_without_type = [row_data[0], row_data[-2], row_data[-1]]
    
                            if all(row_data):
                                if prev_row is None or row_data_without_type != prev_row_data_without_type:
                                    all_data.append(row_data)
                                    prev_row = row_data
                                    prev_row_data_without_type = row_data_without_type
                                else:
                                    print(f"Same row as previous, not appended: {row_data}")
                                    skipped_count += 1
                            else:
                                print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
                                skipped_count += 1
    
                    all_data_with_klient_and_count = [[klient_name] + row + [skipped_count] for row in all_data]
                    df_data = pd.DataFrame(all_data_with_klient_and_count, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca", "Skipped Count"])
                    all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
        # Filter the DataFrame to separate rows with status "nowe".
        filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
        # Format emails before grouping
        filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
        # Group the DataFrame by 'Wykonawca' (email address)
        grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
        # Add a count column to filtered_df_nowe
        filtered_df_nowe['Count'] = filtered_df_nowe.groupby('Wykonawca')['Wykonawca'].transform('size')
    
        # Save the filtered DataFrame to a separate CSV file.
        filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index().to_csv("filtered_nowe_data.csv", index=False)
        print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
        # Now, remove rows with None 'Wykonawca' before creating messages
        filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    
    # Make sure to define base_domains, df_list, driver, and initiate driver properly before calling this function.
    

    This code introduces a skipped_count variable that increments each time a row is skipped. This count is then added as a new column Skipped Count in the df_data DataFrame. Additionally, a Count column is added to the filtered_df_nowe DataFrame to indicate how many texts were published by each Wykonawca.

  3. User

    i mean i want count skipped, but with its original row, so i count how much are this same rows, so you need to count also first one and than skipped

  4. Assistant

    I understand now. You want to count the total number of duplicated rows (including the first instance) and append this count to the DataFrame. Let's modify the function to keep track of these counts more accurately.

    Here's the updated code with the requested feature:

    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from bs4 import BeautifulSoup
    
    def format_email(email):
        # Assuming format_email function is defined elsewhere
        return email.strip().lower()
    
    def function10():
        all_data_df = pd.DataFrame()
    
        for domain, df in zip(base_domains, df_list):
            # List of URLs to scrape
            urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
            for idx, url in enumerate(urls):
                all_data = []
    
                driver.get(url)
                div_content_driver = driver.find_element(By.ID, "prepared_content")
    
                select_element = div_content_driver.find_element(By.XPATH, ".//select")
                select = Select(select_element)
    
                while select.first_selected_option.get_attribute('value') != "-1":
                    select.select_by_value("-1")
    
                html_content = driver.page_source
                soup = BeautifulSoup(html_content, "html.parser")
    
                div_content = soup.find("div", {"id": "prepared_content"})
                table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None # find table in each page
    
                if table:
                    data = []
                    prev_row = None
                    prev_row_data_without_type = None
                    first_row = True
    
                    budget_div = soup.find("div", class_="head")
                    if budget_div:
                        h5_element = budget_div.find("h5")
                        if h5_element:
                            klient_name = h5_element.text.split(" - Budżet")[0].strip()
                        else:
                            klient_name = "Unknown Client"
                    else:
                        klient_name = "Unknown Client"
    
                    row_count = {}
                    
                    for tr in table.find_all("tr"):
                        if first_row:
                            first_row = False
                            continue 
    
                        tds = tr.find_all("td")   
    
                        if len(tds) >= 6:
                            row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                            row_data_without_type = tuple([row_data[0], row_data[-2], row_data[-1]])
    
                            if all(row_data):
                                if row_data_without_type not in row_count:
                                    row_count[row_data_without_type] = 1
                                    all_data.append(row_data)
                                else:
                                    row_count[row_data_without_type] += 1
                                    all_data.append(row_data)
                            else:
                                print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    
                    all_data_with_klient_and_count = [[klient_name] + row + [row_count[tuple([row[0], row[2], row[3]])]] for row in all_data]
                    df_data = pd.DataFrame(all_data_with_klient_and_count, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca", "Duplicate Count"])
                    all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
        # Filter the DataFrame to separate rows with status "nowe".
        filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
        # Format emails before grouping
        filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
        # Group the DataFrame by 'Wykonawca' (email address)
        grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
        # Add a count column to filtered_df_nowe
        filtered_df_nowe['Count'] = filtered_df_nowe.groupby('Wykonawca')['Wykonawca'].transform('size')
    
        # Save the filtered DataFrame to a separate CSV file.
        grouped = filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index()
        grouped.to_csv("filtered_nowe_data.csv", index=False)
        print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
        # Now, remove rows with None 'Wykonawca' before creating messages
        filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    
    # Make sure to define base_domains, df_list, driver, and initiate driver properly before calling this function.
    

    Changes made:

    1. Introduced a dictionary row_count to keep track of the number of times each row (excluding its type) appears.
    2. Each time a unique row pattern is encountered, it initializes its count in the dictionary.
    3. When a duplicate is detected, it increases its count and appends it to the list.
    4. After processing the table, it creates df_data with an additional column Duplicate Count showing how many times each row was merged (including the first instance).
    5. Finally, with the filtered_df_nowe, it groups by 'Wykonawca' and 'Klient' to save the counted and filtered data into a CSV file.
  5. User

    No this is not what im looking for, i want to count all rows, but add count once at first element, i want to do the job as its now, and than add count, so for exmaple i have 5 same rows, first is added to a file, 4 are skipped i want just this one rows with count number in last column. What you provided dosent work as that so here is function which you should modify from begginging

    def function10(): all_data_df = pd.DataFrame()

    for domain, df in zip(base_domains, df_list):
        # List of URLs to scrape
        urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
        for idx, url in enumerate(urls):
            all_data = []
    
            driver.get(url)
            div_content_driver = driver.find_element(By.ID, "prepared_content")
    
            select_element = div_content_driver.find_element(By.XPATH, ".//select")
            select = Select(select_element)
    
            while select.first_selected_option.get_attribute('value') != "-1":
                select.select_by_value("-1")
    
            html_content = driver.page_source
            soup = BeautifulSoup(html_content, "html.parser")
    
            div_content = soup.find("div", {"id": "prepared_content"})
            table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None # find table in each page
    
            if table:
                data = []
                prev_row = None
                first_row = True
    
                budget_div = soup.find("div", class_="head")
                if budget_div:
                    h5_element = budget_div.find("h5")
                    if h5_element:
                        klient_name = h5_element.text.split(" - Budżet")[0].strip()
                    else:
                        klient_name = "Unknown Client"
                else:
                    klient_name = "Unknown Client"
    
                for tr in table.find_all("tr"):
                    if first_row:
                        first_row = False
                        continue 
    
                    tds = tr.find_all("td")   
    
                    if len(tds) >= 6:
                        row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                        row_data_without_type = [row_data[0], row_data[-2], row_data[-1]]
    
                        if all(row_data):
                            if prev_row is None or row_data_without_type != prev_row_data_without_type:
                                all_data.append(row_data)
                                prev_row = row_data
                                prev_row_data_without_type = row_data_without_type
                            else:
                                print(f"Same row as previous, not appended: {row_data}")
                        else:
                            print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    
                all_data.extend(data)
                all_data_with_klient = [[klient_name] + row for row in all_data]
                df_data = pd.DataFrame(all_data_with_klient, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca"])
                all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
    # Filter the DataFrame to separate rows with status "nowe".
    filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
    # Format emails before grouping
    filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
    # Group the DataFrame by 'Wykonawca' (email address)
    grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
    # Save the filtered DataFrame to a separate CSV file.
    # if you want use groupby usage is: .groupby(['Wykonawca', 'Klient']).sum().reset_index() or to_csv dosent work
    filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index().to_csv("filtered_nowe_data.csv", index=False)
    print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
    # Now, filter out rows with None 'Wykonawca' before creating messages
    filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    
    # Loop through each group and write messages to separate files
    for email, group in grouped_df:
        if email is not None:
            with open(f"messages_{email.replace('@', '_')}.txt", "w") as f:
                for index, row in group.iterrows():
                    # Split the date string into components
                    day, month_name, year, time = row['Data dodania'].split()
                    month = month_mapping[month_name]
    
                    # Reconstruct the date string with numerical month
                    date_str = f"{year}-{month:02d}-{day} {time}"
                    
                    # Parse the datetime string and format it to show only the date
                    date_only = datetime.strptime(date_str, "%Y-%m-%d %H:%M").strftime("%d.%m.%Y")
    
                    message = (
                        f"Klient {row['Klient']} został zlecony "
                        f"{date_only} "
                        f"i nie został jeszcze ukończony.\n"
                    )
                    f.write(message)
    
            print(f"Messages for {email} written to messages_{email.replace('@', '_')}.txt")
    
    # Save the combined DataFrame to the main CSV file.
    all_data_df.to_csv("combined_scraped_data.csv", index=False)
    print("All data saved to combined_scraped_data.csv")
    

    Run the function10

    function10()

  6. Assistant

    Got it. If the goal is to count all rows (including the ones that were skipped) but only add the count to the instance of the row, we can keep track of the number of each unique row encountered in a dictionary. Then, after processing the table, we can append the count to the first occurrence of each unique row.

    Here's how you can adjust your function to achieve this:

    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    # Assuming format_email and month_mapping are defined somewhere
    
    def format_email(email):
        return email.strip().lower()
    
    month_mapping = {
        "January": 1, "February": 2, "March": 3, "April": 4,
        "May": 5, "June": 6, "July": 7, "August": 8,
        "September": 9, "October": 10, "November": 11, "December": 12
    }
    
    def function10():
        all_data_df = pd.DataFrame()
    
        for domain, df in zip(base_domains, df_list):
            # List of URLs to scrape
            urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
            for idx, url in enumerate(urls):
                all_data = []
                row_counter = {}
    
                driver.get(url)
                div_content_driver = driver.find_element(By.ID, "prepared_content")
    
                select_element = div_content_driver.find_element(By.XPATH, ".//select")
                select = Select(select_element)
    
                while select.first_selected_option.get_attribute('value') != "-1":
                    select.select_by_value("-1")
    
                html_content = driver.page_source
                soup = BeautifulSoup(html_content, "html.parser")
    
                div_content = soup.find("div", {"id": "prepared_content"})
                table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None # find table in each page
    
                if table:
                    data = []
                    prev_row_data_without_type = None
                    first_row = True
    
                    budget_div = soup.find("div", class_="head")
                    if budget_div:
                        h5_element = budget_div.find("h5")
                        if h5_element:
                            klient_name = h5_element.text.split(" - Budżet")[0].strip()
                        else:
                            klient_name = "Unknown Client"
                    else:
                        klient_name = "Unknown Client"
    
                    for tr in table.find_all("tr"):
                        if first_row:
                            first_row = False
                            continue 
    
                        tds = tr.find_all("td")   
    
                        if len(tds) >= 6:
                            row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                            row_data_without_type = tuple([row_data[0], row_data[2], row_data[3]])
    
                            if all(row_data):
                                if row_data_without_type not in row_counter:
                                    row_counter[row_data_without_type] = 0
                                row_counter[row_data_without_type] += 1
                                
                                if prev_row_data_without_type != row_data_without_type:
                                    all_data.append(row_data)
                                    prev_row_data_without_type = row_data_without_type
                                else:
                                    print(f"Same row as previous, not appended: {row_data}")
                            else:
                                print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    
                    all_data_with_klient_and_count = [
                        [klient_name] + row + [row_counter[tuple([row[0], row[2], row[3]])]] 
                        for row in all_data
                    ]
                    df_data = pd.DataFrame(all_data_with_klient_and_count, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca", "Duplicate Count"])
                    all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
        # Filter the DataFrame to separate rows with status "nowe".
        filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
        # Format emails before grouping
        filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
        # Group the DataFrame by 'Wykonawca' (email address)
        grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
        # Add a count column to filtered_df_nowe
        filtered_df_nowe['Count'] = filtered_df_nowe.groupby('Wykonawca')['Wykonawca'].transform('size')
    
        # Save the filtered DataFrame to a separate CSV file.
        filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index().to_csv("filtered_nowe_data.csv", index=False)
        print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
        # Now, filter out rows with None 'Wykonawca' before creating messages
        filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    
        # Loop through each group and write messages to separate files
        for email, group in grouped_df:
            if email is not None:
                with open(f"messages_{email.replace('@', '_')}.txt", "w") as f:
                    for index, row in group.iterrows():
                        # Split the date string into components
                        day, month_name, year, time = row['Data dodania'].split()
                        month = month_mapping[month_name]
    
                        # Reconstruct the date string with numerical month
                        date_str = f"{year}-{month:02d}-{day} {time}"
                        
                        # Parse the datetime string and format it to show only the date
                        date_only = datetime.strptime(date_str, "%Y-%m-%d %H:%M").strftime("%d.%m.%Y")
    
                        message = (
                            f"Klient {row['Klient']} został zlecony "
                            f"{date_only} "
                            f"i nie został jeszcze ukończony.\n"
                        )
                        f.write(message)
    
                print(f"Messages for {email} written to messages_{email.replace('@', '_')}.txt")
    
        # Save the combined DataFrame to the main CSV file.
        all_data_df.to_csv("combined_scraped_data.csv", index=False)
        print("All data saved to combined_scraped_data.csv")
    
    # Run the function10
    function10()
    

    Changes made:

    1. Kept a dictionary row_counter to track the occurrence of each unique row data.
    2. When processing each row, updated the row_counter for each unique row pattern.
    3. Added a new column Duplicate Count only in the first occurrence of each unique row pattern.
    4. Continued with the existing filtering and file writing logic as before.

    This will ensure that only the first instance of each row has the count of how many times that row pattern appeared, with a column Duplicate Count showing the total count of that row pattern.

  7. User

    if all(row_data): if row_data_without_type not in row_counter: row_counter[row_data_without_type] = 0 row_counter[row_data_without_type] += 1

                                if prev_row_data_without_type != row_data_without_type:
                                    all_data.append(row_data)
                                    prev_row_data_without_type = row_data_without_type
                                else:
                                    print(f"Same row as previous, not appended: {row_data}")
                            else:
                                print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    

    im getting here if prev_row_data_without_type != row_data_without_type: IndentationError: unexpected indent

  8. Assistant

    I see the issue. The indentation error was caused by misaligned indentation in the conditional blocks. Here's the corrected function:

    import pandas as pd
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import Select
    from bs4 import BeautifulSoup
    from datetime import datetime
    
    # Assuming format_email and month_mapping are defined somewhere
    
    def format_email(email):
        return email.strip().lower()
    
    month_mapping = {
        "January": 1, "February": 2, "March": 3, "April": 4,
        "May": 5, "June": 6, "July": 7, "August": 8,
        "September": 9, "October": 10, "November": 11, "December": 12
    }
    
    def function10():
        all_data_df = pd.DataFrame()
    
        for domain, df in zip(base_domains, df_list):
            # List of URLs to scrape
            urls = [f"https://{domain}/{slug}" for slug in df["URL"][:-1]]
    
            for idx, url in enumerate(urls):
                all_data = []
                row_counter = {}
    
                driver.get(url)
                div_content_driver = driver.find_element(By.ID, "prepared_content")
    
                select_element = div_content_driver.find_element(By.XPATH, ".//select")
                select = Select(select_element)
    
                while select.first_selected_option.get_attribute('value') != "-1":
                    select.select_by_value("-1")
    
                html_content = driver.page_source
                soup = BeautifulSoup(html_content, "html.parser")
    
                div_content = soup.find("div", {"id": "prepared_content"})
                table = div_content.find("table", {"class": "opt_task_table"}) if div_content else None  # find table in each page
    
                if table:
                    data = []
                    prev_row_data_without_type = None
                    first_row = True
    
                    budget_div = soup.find("div", class_="head")
                    if budget_div:
                        h5_element = budget_div.find("h5")
                        if h5_element:
                            klient_name = h5_element.text.split(" - Budżet")[0].strip()
                        else:
                            klient_name = "Unknown Client"
                    else:
                        klient_name = "Unknown Client"
    
                    for tr in table.find_all("tr"):
                        if first_row:
                            first_row = False
                            continue
    
                        tds = tr.find_all("td")
    
                        if len(tds) >= 6:
                            row_data = [tds[i].get_text().strip() for i in [1, 2, 4, 5]]
                            row_data_without_type = tuple([row_data[0], row_data[2], row_data[3]])
    
                            if all(row_data):
                                if row_data_without_type not in row_counter:
                                    row_counter[row_data_without_type] = 0
                                row_counter[row_data_without_type] += 1
    
                                if prev_row_data_without_type != row_data_without_type:
                                    all_data.append(row_data)
                                    prev_row_data_without_type = row_data_without_type
                                else:
                                    print(f"Same row as previous, not appended: {row_data}")
                            else:
                                print(f"Row skipped due to missing columns: {[td.get_text().strip() for td in tds]}")
    
                    all_data_with_klient_and_count = [
                        [klient_name] + row + [row_counter[tuple([row[0], row[2], row[3]])]]
                        for row in all_data
                    ]
                    df_data = pd.DataFrame(all_data_with_klient_and_count, columns=["Klient", "Zadanie tekstowe", "Data dodania", "Status", "Wykonawca", "Duplicate Count"])
                    all_data_df = pd.concat([all_data_df, df_data], ignore_index=True)
    
        # Filter the DataFrame to separate rows with status "nowe".
        filtered_df_nowe = all_data_df[all_data_df['Status'] == 'nowe']
    
        # Format emails before grouping
        filtered_df_nowe['Wykonawca'] = filtered_df_nowe['Wykonawca'].apply(format_email)
    
        # Group the DataFrame by 'Wykonawca' (email address)
        grouped_df = filtered_df_nowe.groupby('Wykonawca')
    
        # Add a count column to filtered_df_nowe
        filtered_df_nowe['Count'] = filtered_df_nowe.groupby('Wykonawca')['Wykonawca'].transform('size')
    
        # Save the filtered DataFrame to a separate CSV file.
        filtered_df_nowe.groupby(['Wykonawca', 'Klient']).sum().reset_index().to_csv("filtered_nowe_data.csv", index=False)
        print("Filtered 'nowe' data saved to filtered_nowe_data.csv")
    
        # Now, filter out rows with None 'Wykonawca' before creating messages
        filtered_df_nowe = filtered_df_nowe.dropna(subset=['Wykonawca'])
    
        # Loop through each group and write messages to separate files
        for email, group in grouped_df:
            if email is not None:
                with open(f"messages_{email.replace('@', '_')}.txt", "w") as f:
                    for index, row in group.iterrows():
                        # Split the date string into components
                        day, month_name, year, time = row['Data dodania'].split()
                        month = month_mapping[month_name]
    
                        # Reconstruct the date string with numerical month
                        date_str = f"{year}-{month:02d}-{day} {time}"
                        
                        # Parse the datetime string and format it to show only the date
                        date_only = datetime.strptime(date_str, "%Y-%m-%d %H:%M").strftime("%d.%m.%Y")
    
                        message = (
                            f"Klient {row['Klient']} został zlecony "
                            f"{date_only} "
                            f"i nie został jeszcze ukończony.\n"
                        )
                        f.write(message)
    
                print(f"Messages for {email} written to messages_{email.replace('@', '_')}.txt")
    
        # Save the combined DataFrame to the main CSV file.
        all_data_df.to_csv("combined_scraped_data.csv", index=False)
        print("All data saved to combined_scraped_data.csv")
    
    # Run the function10
    function10()
    

    In this corrected version:

    1. Fixed the indentation in the conditional blocks within the loop that processes table rows.
    2. The row counter logic counts all occurrences and properly appends the first unique instance with the count of duplicates.

    This should resolve the indentation errors and meet your requirement of counting all duplicates while only adding the count to the first instance of each unique row.