LuTong hai 3 meses
pai
achega
ad7beef675
Modificáronse 1 ficheiros con 15 adicións e 18 borrados
  1. 15 18
      src/scraper.py

+ 15 - 18
src/scraper.py

@@ -15,8 +15,8 @@ except ImportError:
 
 import time, random, re, os, subprocess, urllib.parse, json, traceback, socket
 from selenium import webdriver
-from selenium.webdriver.edge.options import Options as EdgeOptions
-from selenium.webdriver.edge.service import Service as EdgeService
+from selenium.webdriver.chrome.options import Options as ChromeOptions
+from selenium.webdriver.chrome.service import Service as ChromeService
 import undetected_chromedriver as uc 
 from selenium.webdriver.common.by import By
 from selenium.webdriver.common.action_chains import ActionChains
@@ -38,7 +38,6 @@ class Scraper1688:
             self._cleanup_processes()
             
             # 3. 后台启动 Edge (带调试端口)
-            # 使用独立的用户数据目录,避免和日常使用的 Edge 冲突
             edge_user_data = os.path.join(os.getcwd(), "1688_edge_profile")
             cmd = [
                 edge_path,
@@ -51,36 +50,34 @@ class Scraper1688:
                 cmd.append("--headless")
             
             try:
-                # 异步启动浏览器进程
                 subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
                 time.sleep(3) # 等待浏览器初始化
                 
-                # 4. 接管 Edge
-                opts = EdgeOptions()
+                # 4. 关键:使用 Chrome 类接管 Edge
+                # 这样可以解决 selenium-stealth 只支持 Chrome 类的问题
+                opts = ChromeOptions()
                 opts.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
                 
-                # 尝试连接
                 try:
-                    # 优先尝试使用系统路径中的驱动
-                    self.driver = webdriver.Edge(options=opts)
-                    print("[+] Edge 浏览器已成功自动弹出并接管!")
+                    self.driver = webdriver.Chrome(options=opts)
+                    print("[+] Edge 浏览器已通过 Chrome 类接管成功!")
                 except:
-                    # 如果连不上,尝试使用 webdriver_manager 自动下载匹配驱动
-                    print("[*] 尝试自动下载匹配的 EdgeDriver...")
-                    from webdriver_manager.microsoft import EdgeChromiumDriverManager
-                    service = EdgeService(EdgeChromiumDriverManager().install())
-                    self.driver = webdriver.Edge(service=service, options=opts)
-                    print("[+] Edge 浏览器已通过驱动管理自动弹出并接管!")
+                    print("[*] 尝试自动下载匹配的驱动接管...")
+                    from webdriver_manager.chrome import ChromeDriverManager
+                    service = ChromeService(ChromeDriverManager().install())
+                    self.driver = webdriver.Chrome(service=service, options=opts)
+                    print("[+] Edge 浏览器已成功接管!")
                     
             except Exception as e:
-                print(f"[*] Edge 自动接管模式失败,准备回退到 Chrome: {e}")
+                print(f"[*] Edge 自动接管模式失败,准备回退: {e}")
         
-        # 5. 兜底方案:如果 Edge 启动或接管失败,启动 Chrome
+        # 5. 兜底方案
         if not self.driver:
             print("[*] 正在启动 Chrome (undetected-chromedriver) 模式...")
             self._init_chrome(headless)
 
         if self.driver:
+            # 此时 self.driver 无论是接管还是自启,都是 Chrome 类型,不会报错
             stealth(self.driver, languages=["zh-CN", "zh"], vendor="Google Inc.", platform="Win32", fix_hairline=True)
 
     def _find_edge(self):