from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By import time from PIL import Image from io import BytesIO import base64 import pytesseract import traceback import subprocess pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe' def save_aspxauth_cookie(driver, filename_aspxauth, filename_session): aspxauth_cookie_value = None session_cookie_value = None cookies = driver.get_cookies() for cookie in cookies: if cookie['name'] == '.ASPXAUTH': aspxauth_cookie_value = cookie['value'] elif cookie['name'] == 'ASP.NET_SessionId': session_cookie_value = cookie['value'] if aspxauth_cookie_value: with open(filename_aspxauth, 'w') as f: f.write(aspxauth_cookie_value) if session_cookie_value: with open(filename_session, 'w') as f: f.write(session_cookie_value) def perform_ocr(image): try: text = pytesseract.image_to_string(image) return text except Exception as e: print(f"OCR Hatası: {e}") traceback.print_exc() return None def main(): site_url = "https://arackiralama.egm.gov.tr/" cookies_file_aspxauth = "cookies_aspxauth.txt" cookies_file_session = "cookies_session.txt" max_iterations = 10 username = "MAİL" password = "ŞİFRE" chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-javascript") try: iteration = 1 while iteration <= max_iterations: driver = webdriver.Chrome(options=chrome_options) # Chrome'u başlatırken bekleme süresi ekleniyor driver.set_page_load_timeout(60) try: driver.get(site_url) except Exception as e: print(f"Sayfa yüklenirken bir hata oluştu: {e}") try: tamam_button = driver.find_element(By.CLASS_NAME, "ui-button-text") tamam_button.click() print("Login page closed") time.sleep(1) except Exception as e: print(f"Login page not closed: {e}") time.sleep(5) username_input = driver.find_element(By.ID, "username") username_input.send_keys(username) print(f"Username '{username}' entered.") time.sleep(1) password_input = driver.find_element(By.ID, "password") password_input.send_keys(password) print("Password entered.") time.sleep(1) captcha_img_element = driver.find_element(By.ID, "imgKod") captcha_img_base64 = captcha_img_element.get_attribute('src').split(",")[1] image_data = base64.b64decode(captcha_img_base64) captcha_img = Image.open(BytesIO(image_data)) ocr_result = perform_ocr(captcha_img) if ocr_result: print("Captcha: ") print(ocr_result) captcha_input = driver.find_element(By.ID, "txtCaptcha") captcha_input.send_keys(ocr_result) time.sleep(5) save_aspxauth_cookie(driver, cookies_file_aspxauth, cookies_file_session) else: print("Text could not be extracted with OCR.") captcha_img.save(f'captcha_img_{iteration}.png') driver.quit() time.sleep(100) iteration += 1 except Exception as e: print(f"An error occurred: {e}") traceback.print_exc() finally: try: driver.quit() except Exception as e: print(f"Tarayıcı kapatılırken bir hata oluştu: {e}") if __name__ == "__main__": subprocess.run(["python", "plaka.py"]) main()