06年老电脑复活Ubuntu14.04配置Python网站爬自动化

吃灰电脑经过几天的折腾,首先放弃了Chrome方案,因为谷歌一早就不支持32位构架了,所以不是浏览器不行,就是驱动不行,最后放弃。

电脑配置:赛扬M440,1.5G内存,100G硬盘

系统配置:ubuntu 14.04(I386)32位

浏览器:Firefox 版本是 66.0.3(32位)

浏览器驱动:适配 Firefox 66.0.3 且支持 Linux 32 位的 GeckoDriver 0.24.0 版本(最后一个支持 32 位 Linux 的 GeckoDriver 版本)

步骤 1:下载适配的 GeckoDriver 0.24.0(32 位)

先删除旧的 GeckoDriver(若已下载),再下载新的适配版本:

bash 复制代码
# 1. 移除旧版本(若有)
sudo rm -f /usr/local/bin/geckodriver ~/geckodriver

# 2. 下载GeckoDriver 0.24.0(32位Linux,适配Firefox 66)
cd ~ && wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz -O geckodriver240.tar.gz

# 若github访问失败,用国内镜像:
# wget https://npmmirror.com/mirrors/geckodriver/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz -O geckodriver240.tar.gz

# 3. 解压并配置全局可用
tar -zxvf geckodriver240.tar.gz
chmod +x geckodriver
sudo mv geckodriver /usr/local/bin/

# 4. 验证版本
geckodriver --version

✅ 成功标志:输出geckodriver 0.24.0 ( 2019-01-28)

步骤 2:修改验证脚本(适配 Firefox 66.0.3)

Firefox 66 对参数的支持与 56.x 略有不同,调整脚本参数:

bash 复制代码
nano ~/test_selenium.py

适配脚本:

bash 复制代码
# Ubuntu 14.04 + Firefox 66.0.3 + GeckoDriver 0.24.0 验证脚本
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
import sys

# 强制修复urllib3超时参数(解决类型错误)
import urllib3
urllib3.util.timeout.Timeout.DEFAULT_TIMEOUT = 30
# 替换urllib3超时验证逻辑(兜底)
original_validate = urllib3.util.timeout.Timeout._validate_timeout
def patched_validate(self, value, name):
    try:
        return original_validate(self, int(value) if value else 30, name)
    except:
        return original_validate(self, 30, name)
urllib3.util.timeout.Timeout._validate_timeout = patched_validate

def main():
    try:
        # 1. Firefox 66专属配置(适配32位低配置)
        firefox_options = Options()
        firefox_options.add_argument('--headless')  # 无界面模式(必加)
        firefox_options.add_argument('--no-sandbox') # Ubuntu 14.04必加
        firefox_options.add_argument('--disable-gpu') # 禁用GPU(老旧CPU)
        # 关闭无用功能,减少资源占用
        firefox_options.set_preference('browser.startup.page', 0)
        firefox_options.set_preference('browser.startup.homepage', 'about:blank')
        firefox_options.set_preference('network.http.timeout', 30000) # 网络超时30秒

        # 2. 手动指定GeckoDriver路径,避免自动查找冲突
        gecko_driver_path = '/usr/local/bin/geckodriver'
        service = Service(executable_path=gecko_driver_path)

        # 3. 创建Firefox实例(适配66版本)
        driver = webdriver.Firefox(
            service=service,
            options=firefox_options
        )
        driver.set_page_load_timeout(30)
        print("✅ GeckoDriver 0.24.0与Firefox 66.0.3连接成功!")

        # 4. 访问本地空白页(无网络/SSL冲突)
        driver.get("about:blank")
        print(f"✅ 空白页访问成功,标题:{driver.title}")

        # 可选:访问HTTP百度(避开HTTPS,适配老旧SSL库)
        # driver.get("http://www.baidu.com")
        # print(f"✅ 百度访问成功,标题:{driver.title}")

        # 5. 关闭浏览器
        driver.quit()
        service.stop()
        print("🎉 自动化程序验证成功!")

    except Exception as e:
        print(f"❌ 验证失败,错误:{str(e)}")
        # 版本检查(关键)
        try:
            import subprocess
            print("\n🔍 版本信息:")
            print("Firefox:", subprocess.check_output(['firefox', '--version']).decode().strip())
            print("GeckoDriver:", subprocess.check_output(['geckodriver', '--version']).decode().strip())
            print("Selenium:", __import__('selenium').__version__)
        except:
            pass
        sys.exit(1)

if __name__ == "__main__":
    main()

保存退出:Ctrl+O → 回车 → Ctrl+X

步骤 3:运行验证脚本

bash 复制代码
python3 ~/test_selenium.py

✅ 成功标志(终端输出)

bash 复制代码
✅ GeckoDriver 0.24.0与Firefox 66.0.3连接成功!
✅ 空白页访问成功,标题:about:blank
🎉 自动化程序验证成功!

❌ 常见报错及解决

  1. SessionNotCreatedException :版本仍不匹配 → 确认geckodriver --version是 0.24.0,firefox --version是 66.0.3;
  2. Permission denied :GeckoDriver 无执行权限 → 重新执行chmod +x /usr/local/bin/geckodriver
  3. Timeout :网络 DNS 问题 → 仅访问about:blank即可,无需访问百度。

总结

  1. 核心适配:Firefox 66.0.3 必须搭配 GeckoDriver 0.24.0(支持 32 位 Linux 的版本);
  2. 关键修改:脚本中用Service手动启动 GeckoDriver,修复 urllib3 超时参数逻辑;
  3. 避坑点:优先访问本地空白页,避开 HTTPS/SSL 的老旧库冲突,确保低配置 CPU 能运行。
相关推荐
chatexcel28 分钟前
元空AI+Clawdbot:7×24 AI办公智能体新形态详解(长期上下文/自动化任务/工具粘合)
运维·人工智能·自动化
青岑CTF38 分钟前
攻防世界-Ics-05-胎教版wp
开发语言·安全·web安全·网络安全·php
Li emily39 分钟前
如何通过外汇API平台快速实现实时数据接入?
开发语言·python·api·fastapi·美股
m0_561359671 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
Ulyanov1 小时前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲
APIshop1 小时前
Java 实战:调用 item_search_tmall 按关键词搜索天猫商品
java·开发语言·数据库
血小板要健康1 小时前
Java基础常见面试题复习合集1
java·开发语言·经验分享·笔记·面试·学习方法
淼淼7632 小时前
安装jdk1.8
java·开发语言
2401_838472512 小时前
使用Python进行图像识别:CNN卷积神经网络实战
jvm·数据库·python
PfCoder2 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform