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

133 lines
3.0 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 快速入门指南
## 快速安装(推荐)
使用自动安装脚本:
```bash
cd ~/project/jdpl # 进入项目目录
chmod +x jd/setup_ubuntu.sh
./jd/setup_ubuntu.sh
```
脚本会自动:
1. ✅ 检查并安装 Python3 和依赖
2. ✅ 检查并安装 Chrome/Chromium
3. ✅ 安装 Chrome 运行时依赖
4. ✅ 创建 Python 虚拟环境
5. ✅ 安装 DrissionPage
6. ✅ 创建便捷运行脚本
## 手动安装
### 1. 安装 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
sudo apt install -y chromium-browser
```
### 2. 安装系统依赖
```bash
sudo apt update
sudo apt install -y python3 python3-pip python3-venv \
libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 \
libxcomposite1 libxdamage1 libxfixes3 libxrandr2 \
libgbm1 libasound2
```
### 3. 创建虚拟环境
```bash
cd ~/project/jdpl
python3 -m venv venv
source venv/bin/activate
pip install DrissionPage
deactivate
```
### 4. 运行脚本
```bash
# 方式1: 使用便捷脚本(如果运行了 setup_ubuntu.sh
./run_logistics.sh
# 方式2: 手动运行
source venv/bin/activate
python jd/fetch_logistics_ubuntu.py
deactivate
```
## 常见问题
### Q: 遇到 "externally-managed-environment" 错误?
A: 这是 Ubuntu 22.04+ 的保护机制。**必须使用虚拟环境**,不要使用 `--break-system-packages`
### Q: 虚拟环境在哪里?
A: 在项目目录下的 `venv` 文件夹。每次运行前需要激活:`source venv/bin/activate`
### Q: 找不到 Chrome
A: 脚本会自动查找,也可以手动安装。常见路径:
- `/usr/bin/google-chrome`
- `/usr/bin/chromium-browser`
### Q: 无头模式 vs 有界面模式?
A: 在 `fetch_logistics_ubuntu.py` 中修改:
```python
USE_HEADLESS = True # 无头模式(服务器环境)
USE_HEADLESS = False # 有界面模式(需要图形界面)
```
### Q: 如何修改默认 URL
A: 编辑 `fetch_logistics_ubuntu.py`,找到:
```python
tracking_url = "https://3.cn/2t-Iibig"
```
修改为你想要的 URL。
## 验证安装
运行测试:
```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()
"
deactivate
```
## 项目结构
```
jdpl/
├── jd/
│ ├── fetch_logistics_ubuntu.py # Ubuntu 主脚本
│ ├── setup_ubuntu.sh # 自动安装脚本
│ └── UBUNTU_SETUP.md # 详细文档
├── venv/ # Python 虚拟环境(运行脚本后创建)
└── run_logistics.sh # 便捷运行脚本(运行 setup 后创建)
```