|
19 | 19 | from selenium.common.exceptions import ( |
20 | 20 | TimeoutException, |
21 | 21 | NoSuchElementException, |
| 22 | + NoSuchWindowException, |
22 | 23 | StaleElementReferenceException, |
| 24 | + WebDriverException, |
23 | 25 | ) |
24 | 26 | from webdriver_manager.chrome import ChromeDriverManager |
25 | 27 |
|
@@ -983,6 +985,7 @@ def scrape_by_count(self, count: int) -> List[Tweet]: |
983 | 985 | max_recovery_attempts = 3 |
984 | 986 | scan_cycles = 0 |
985 | 987 | max_scan_cycles = max(60, count * 8) |
| 988 | + browser_lost = False |
986 | 989 |
|
987 | 990 | try: |
988 | 991 | while len(self.tweets_collected) < count: |
@@ -1093,9 +1096,40 @@ def scrape_by_count(self, count: int) -> List[Tweet]: |
1093 | 1096 | except KeyboardInterrupt: |
1094 | 1097 | print(f"\n\nDurduruldu! {len(self.tweets_collected)} tweet toplandı.") |
1095 | 1098 | raise # Ana programa ilet |
| 1099 | + except NoSuchWindowException as e: |
| 1100 | + browser_lost = True |
| 1101 | + print(f"\nBrowser penceresi kapandı veya Chrome bağlantısı koptu. {len(self.tweets_collected)} tweet kısmi sonuç olarak kullanılacak.") |
| 1102 | + record_event( |
| 1103 | + self.run_log, |
| 1104 | + "browser", |
| 1105 | + "error" if not self.tweets_collected else "warning", |
| 1106 | + f"Browser window closed during count scrape: {e}", |
| 1107 | + reason="browser_window_closed", |
| 1108 | + collected=len(self.tweets_collected), |
| 1109 | + target=count, |
| 1110 | + scan_cycles=scan_cycles, |
| 1111 | + ) |
| 1112 | + except WebDriverException as e: |
| 1113 | + if "no such window" not in str(e).lower() and "web view not found" not in str(e).lower(): |
| 1114 | + raise |
| 1115 | + browser_lost = True |
| 1116 | + print(f"\nChrome webview kayboldu. {len(self.tweets_collected)} tweet kısmi sonuç olarak kullanılacak.") |
| 1117 | + record_event( |
| 1118 | + self.run_log, |
| 1119 | + "browser", |
| 1120 | + "error" if not self.tweets_collected else "warning", |
| 1121 | + f"Chrome webview was lost during count scrape: {e}", |
| 1122 | + reason="browser_window_closed", |
| 1123 | + collected=len(self.tweets_collected), |
| 1124 | + target=count, |
| 1125 | + scan_cycles=scan_cycles, |
| 1126 | + ) |
1096 | 1127 |
|
1097 | 1128 | # Scroll bitti, şimdi show more olan tweetlerin tam metnini al |
1098 | | - self._process_show_more_tweets() |
| 1129 | + if browser_lost: |
| 1130 | + print("Browser kapandığı için show more/article tam metin alma adımı atlandı.") |
| 1131 | + else: |
| 1132 | + self._process_show_more_tweets() |
1099 | 1133 |
|
1100 | 1134 | print(f"Toplam {len(self.tweets_collected)} tweet toplandı.") |
1101 | 1135 | if not self.tweets_collected: |
|
0 commit comments