Python Forum

Full Version: Error loop with Chatgpt
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, sorry in advance for the "dumb" question, but I was trying to create a simple script for web scraping contacts from government sites.
I have asked for help in touching up and cleaning up the chatgpt script, but unfortunately it doesn't lead to a result at all. We keep going in circles talking about bad indentation and bad syntax. But even though I have tried about 30 different scripts, it still throws the same error.
Could any of you take a look at this and advise what to do?

Thanks a lot for any help

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

# Automatické stažení správného ChromeDriveru
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# URL stránky s kontaktními informacemi
url = "https://www.bayernportal.de/dokumente/behoerde/40997963439"
driver.get(url)

# Čekání na načtení vyskakovacího okna pro cookies a jeho zavření
try:
    wait = WebDriverWait(driver, 20)
    # Opravený XPath pro tlačítko "Alle cookies akzeptieren"
    cookie_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Alle cookies akzeptieren')]")))
    cookie_button.click()  # Kliknutí na souhlas s cookies
    print("Cookies byly potvrzeny.")
except Exception as e:
    print(f"Vyskakovací okno pro cookies nebylo nalezeno: {e}")
    # Pokusíme se použít JavaScript k odkliknutí tlačítka, pokud není k dispozici
    try:
        script = "document.querySelector('button').click();"
        driver.execute_script(script)
        print("Cookies byly potvrzeny (JavaScript).")
    except Exception as js_error:
        print(f"Chyba při klikání pomocí JavaScriptu: {js_error}")

# Čekání na načtení požadovaných elementů (telefon a e-mail)
wait = WebDriverWait(driver, 20)

# Extrahování telefonního čísla
try:
    phone_element = wait.until(EC.presence_of_element_located((By.XPATH, "//a[starts-with(@href, 'tel:')]")))
    phone_number = phone_element.text.strip() if phone_element else "Nenalezeno"
    print("Telefon:", phone_number)
except Exception as e:
    print(f"Chyba při získávání telefonního čísla: {e}")
    phone_number = "Nenalezeno"

# Extrahování e-mailu
try:
    email_element = wait.until(EC.presence_of_element_located((By.XPATH, "//a[contains(@href, 'mailto:')]")))
    email_address = email_element.text.strip() if email_element else "Nenalezeno"
    print("Email:", email_address)
except Exception as e:
    print(f"Chyba při získávání e-mailu: {e}")
    email_address = "Nenalezeno"

# Zavření prohlížeče
driver.quit()
And here is firther explanation from chatgpt: The issue you are facing is still related to the indentation and the presence of unnecessary escape characters (\) in your code. In Python, indentation must be consistent, and escape characters are not required in the syntax you are using.

Let's go through a few key fixes:

Key Problems:
Indentation Errors: Python requires consistent indentation. Each nested block (like the code in try and except) must be indented the same way. Inconsistent indentation causes the IndentationError.
Escape Characters: You have extra \ (backslash) characters in places where they are not needed. These can cause syntax issues.
Corrected Code:
Here is the corrected version of your code without the indentation issues and escape characters

What has been fixed:
Removed Escape Characters: I removed unnecessary backslashes (\) in places like the XPath queries and string literals.
Corrected Indentation: Ensured consistent indentation throughout the code. Every block inside try, except, and other structures has been indented correctly.
No additional characters: I removed extra backslashes at the end of lines.
This code should now run without errors. If the problem persists or if there are specific exceptions that need attention, feel free to share those details.