Files
tb_pl/jd/UBUNTU_SETUP.md
2026-04-26 13:39:19 +08:00

310 lines
6.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Ubuntu 环境设置指南
## 1. 安装 Google Chrome 或 Chromium
### 方式一:安装 Google Chrome推荐
```bash
# 下载并安装 Google Chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install -y ./google-chrome-stable_current_amd64.deb
```
### 方式二:安装 Chromium开源版本
```bash
sudo apt update
sudo apt install -y chromium-browser
```
## 2. 安装必要的依赖库
```bash
# 更新包列表
sudo apt update
# 安装 Chrome/Chromium 运行时依赖
sudo apt install -y \
libnss3 \
libatk-bridge2.0-0 \
libdrm2 \
libxkbcommon0 \
libxcomposite1 \
libxdamage1 \
libxfixes3 \
libxrandr2 \
libgbm1 \
libasound2 \
libpango-1.0-0 \
libcairo2 \
libatk1.0-0 \
libgdk-pixbuf2.0-0 \
libgtk-3-0
```
## 3. 安装 Python 依赖
⚠️ **注意**: Ubuntu 22.04+ 默认不允许直接使用 pip 安装系统级包。请使用以下方法之一:
### 方法一:使用虚拟环境(推荐)✅
```bash
# 1. 确保已安装 python3-venv
sudo apt install -y python3-venv python3-pip
# 2. 进入项目目录
cd ~/project/jdpl # 或者你的项目路径
# 3. 创建虚拟环境
python3 -m venv venv
# 4. 激活虚拟环境
source venv/bin/activate
# 5. 安装依赖
pip install DrissionPage
# 如果使用数据库
pip install sqlalchemy pymysql
# 6. 运行脚本(需要在虚拟环境中)
python jd/fetch_logistics_ubuntu.py
# 7. 退出虚拟环境(不需要时)
deactivate
```
### 方法二:使用 pipx适合单命令工具
```bash
# 1. 安装 pipx
sudo apt install -y pipx
pipx ensurepath
# 2. 使用 pipx 安装(如果要全局可用)
# 注意pipx 主要用于安装应用程序,不太适合库
```
### 方法三:使用 --break-system-packages不推荐但快速
```bash
# ⚠️ 警告:可能破坏系统 Python 环境,不推荐在生产环境使用
# 安装 DrissionPage
pip3 install --break-system-packages DrissionPage
# 如果使用数据库
pip3 install --break-system-packages sqlalchemy pymysql
```
### 方法四:使用 apt 安装(如果可用)
```bash
# 某些包可能通过 apt 安装(但 DrissionPage 通常不行)
sudo apt install -y python3-drissionpage # 通常不可用
```
**推荐使用方法一(虚拟环境)**,这是最安全和标准的做法。
## 4. 配置说明
### 无头模式Headlessvs 有界面模式
`fetch_logistics_ubuntu.py` 文件中,可以设置 `USE_HEADLESS` 变量:
```python
USE_HEADLESS = True # 无头模式,适合服务器环境,不显示浏览器窗口
USE_HEADLESS = False # 有界面模式,需要图形界面支持
```
### 无头模式使用场景:
- 服务器环境(无桌面环境)
- SSH 远程连接
- Docker 容器
- 需要后台运行
### 有界面模式使用场景:
- 本地 Ubuntu 桌面环境
- 需要调试和查看浏览器行为
- 有 X11 或 Wayland 显示服务器
## 5. 运行脚本
```bash
# 进入脚本目录
cd /path/to/jdpl/jd
# 运行脚本
python3 fetch_logistics_ubuntu.py
```
## 6. 如果遇到问题
### 问题1: 找不到 Chrome/Chromium
```bash
# 检查是否安装
which google-chrome
which chromium-browser
# 如果找不到,检查常见路径
ls -la /usr/bin/google-chrome*
ls -la /usr/bin/chromium*
```
### 问题2: 权限问题
```bash
# 如果提示权限不足,可能需要添加 --no-sandbox 参数
# 脚本中已经自动添加了这个参数
```
### 问题3: 无头模式无法使用
如果设置 `USE_HEADLESS = False` 但仍然无法显示,可能需要:
```bash
# 检查 DISPLAY 环境变量
echo $DISPLAY
# 如果为空,设置显示(如果是本地桌面)
export DISPLAY=:0
# 或者使用 Xvfb虚拟显示
sudo apt install -y xvfb
xvfb-run -a python3 fetch_logistics_ubuntu.py
```
### 问题4: 缺少共享内存
如果看到 `/dev/shm` 相关错误:
```bash
# 检查 /dev/shm 大小
df -h /dev/shm
# 如果太小,可以挂载更大的空间(临时)
sudo mount -o remount,size=2G /dev/shm
```
### 问题5: 依赖库缺失
如果运行时提示缺少某些库:
```bash
# 安装所有可能的依赖
sudo apt install -y \
fonts-liberation \
libappindicator3-1 \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgbm1 \
libgcc1 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libnss3 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
lsb-release \
wget \
xdg-utils
```
## 7. Docker 环境(可选)
如果需要:
```dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
wget \
&& wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& apt-get install -y ./google-chrome-stable_current_amd64.deb \
&& pip3 install DrissionPage
WORKDIR /app
COPY jd/fetch_logistics_ubuntu.py .
CMD ["python3", "fetch_logistics_ubuntu.py"]
```
## 8. 验证安装
### 如果使用虚拟环境:
```bash
# 激活虚拟环境
source venv/bin/activate
# 运行测试
python -c "
from DrissionPage import ChromiumPage, ChromiumOptions
import os
chrome_path = '/usr/bin/google-chrome'
if not os.path.exists(chrome_path):
chrome_path = '/usr/bin/chromium-browser'
options = ChromiumOptions()
options.set_browser_path(chrome_path)
options.headless(True)
page = ChromiumPage(options)
page.get('https://www.baidu.com')
print('✅ 浏览器测试成功!')
page.quit()
"
```
### 如果使用系统级安装(--break-system-packages
```bash
python3 -c "
from DrissionPage import ChromiumPage, ChromiumOptions
import os
chrome_path = '/usr/bin/google-chrome'
if not os.path.exists(chrome_path):
chrome_path = '/usr/bin/chromium-browser'
options = ChromiumOptions()
options.set_browser_path(chrome_path)
options.headless(True)
page = ChromiumPage(options)
page.get('https://www.baidu.com')
print('✅ 浏览器测试成功!')
page.quit()
"
```
## 常见路径总结
Chrome/Chromium 在 Ubuntu 上的常见路径:
- `/usr/bin/google-chrome` - Google Chrome
- `/usr/bin/google-chrome-stable` - Google Chrome (稳定版)
- `/usr/bin/chromium-browser` - Chromium
- `/usr/bin/chromium` - Chromium (简化名)
- `/snap/bin/chromium` - Snap 安装的 Chromium
- `/opt/google/chrome/chrome` - 某些安装方式的路径
脚本会自动检测这些路径。