Python Selenium邮件采集程序在Sublime可运行但终端报错求助
问题:Sublime中可运行的Selenium邮件采集程序在终端执行报错
我开发了一个基于Python Selenium的网站邮件采集程序,编写于Sublime Text编辑器中。程序在Sublime中通过Ctrl+B或「Build with python」执行时可正常运行,但在终端执行python3 harvest.py时,会在初始化Firefox驱动阶段触发错误。
程序代码
from selenium import webdriver from selenium.webdriver import FirefoxOptions class colors: RESET = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BLACK = '\033[30m' RED = '\033[31m' GREEN = '\033[32m' YELLOW = '\033[33m' BLUE = '\033[34m' MAGENTA = '\033[35m' CYAN = '\033[36m' WHITE = '\033[37m' BOLDBLACK = '\033[1;30m' BOLDRED = '\033[1;31m' BOLDGREEN = '\033[1;32m' BOLDYELLOW = '\033[1;33m' BOLDBLUE = '\033[1;34m' BOLDMAGENTA = '\033[1;35m' BOLDCYAN = '\033[1;36m' BOLDWHITE = '\033[1;37m' class App: def __init__(self): self.emails = [] self.opts = FirefoxOptions() self.opts.add_argument("--headless") self.opts.binary_location = "/usr/bin/firefox" self.driver = webdriver.Firefox(options=self.opts) self.read_urls() self.driver.close() def read_urls(self): urls = [ "https://wertweoritwoertiwoerit.com/", "https://rwteirtwertwertwertwe.com/" ] for url in urls: self.scan(url) #print(self.emails) def scan(self,url): self.driver.get(url) source_code = self.driver.page_source body = self.driver.find_element("css selector", "body") visible_text = body.text edited_visible_text = visible_text.replace("\n", " ") array = edited_visible_text.split(' ') for string in array: if "@" in string: self.emails.append(string) print(""+colors.BOLDBLUE + f"[+]{string}"+colors.RESET) if __name__ == "__main__": main = App()
终端执行报错信息
╭─root at kali in ~kali 24-03-24 - 18:58:52 ╰─○ python3 harvest.py Traceback (most recent call last): File "/home/kali/harvest.py", line 54, in <module> main = App() ^^^^^ File "/home/kali/harvest.py", line 34, in __init__ self.driver = webdriver.Firefox(options=self.opts) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/firefox/webdriver.py", line 69, in __init__ super().__init__(command_executor=executor, options=options) File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__ self.start_session(capabilities) File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.11/dist-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
已完成的排查动作
- 配置了Firefox二进制文件路径
- 设置了geckodriver路径并尝试添加相关配置
- 尝试查找geckodriver日志文件但未找到
排查方向与解决方案
1. 确认Python环境一致性
Sublime和终端可能使用不同的Python环境,导致依赖版本差异:
- 在Sublime中执行代码
import sys; print(sys.executable),获取其使用的Python路径 - 在终端执行
which python3,对比两者路径
如果路径不同,可选择:- 在终端切换到Sublime使用的Python环境运行脚本(比如用
/path/to/sublime/python harvest.py) - 在终端的Python环境中重新安装依赖:
pip3 install selenium
- 在终端切换到Sublime使用的Python环境运行脚本(比如用
2. 验证geckodriver与Firefox版本兼容性
版本不匹配是常见报错原因:
- 终端执行
firefox --version和geckodriver --version,记录版本号 - 确保两者版本兼容(最新版geckodriver通常适配最新版Firefox,旧版本需对应匹配),不兼容则升级或降级其中一方
3. 明确指定geckodriver路径
在代码中直接指定geckodriver的完整路径,避免环境变量问题:
from selenium import webdriver from selenium.webdriver import FirefoxOptions from selenium.webdriver.firefox.service import Service # ... class App: def __init__(self): self.emails = [] self.opts = FirefoxOptions() self.opts.add_argument("--headless") self.opts.binary_location = "/usr/bin/firefox" # 添加这行指定geckodriver路径 service = Service(executable_path="/path/to/geckodriver") self.driver = webdriver.Firefox(service=service, options=self.opts) self.read_urls() self.driver.close()
4. 启用geckodriver日志定位问题
修改代码生成日志文件,查看Firefox启动失败的具体原因:
service = Service(executable_path="/path/to/geckodriver", log_path="geckodriver.log") self.driver = webdriver.Firefox(service=service, options=self.opts)
运行脚本后,查看当前目录下的geckodriver.log,里面会包含详细的启动错误信息。
5. 调整无头模式参数
部分Linux环境下,无头模式需要额外参数才能正常运行:
self.opts.add_argument("--headless=new") # 替代旧的--headless参数 self.opts.add_argument("--disable-gpu") self.opts.add_argument("--no-sandbox") # 解决沙箱权限问题 self.opts.add_argument("--disable-dev-shm-usage") # 解决临时空间不足问题
内容的提问来源于stack exchange,提问作者schlechtesterprogrammierer
相关产品推荐
相关产品推荐

