Nintendo Switch 新闻爬虫 #271
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Nintendo Switch 新闻爬虫 | |
| on: | |
| schedule: | |
| - cron: '0 9 * * *' # 每天早上 9 点执行(UTC 时间) | |
| workflow_dispatch: # 支持手动触发 | |
| jobs: | |
| crawl-and-update: | |
| runs-on: ubuntu-22.04 # 明确指定 Ubuntu 22.04 | |
| steps: | |
| # 1. 检出代码 | |
| - name: 检出仓库 | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| # 2. 设置 Python 环境 | |
| - name: 设置 Python 3.10 | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| # 3. 安装系统依赖 | |
| - name: 安装系统依赖 | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y \ | |
| libgl1 \ | |
| libglib2.0-0 \ | |
| libsm6 \ | |
| libxrender1 \ | |
| libxext6 \ | |
| xvfb \ | |
| libasound2 \ | |
| libffi7 \ | |
| libx264-163 \ | |
| jq # 安装 jq 用于 JSON 处理 | |
| sudo apt-get clean | |
| # 4. 安装 Python 依赖 | |
| - name: 安装 Python 包 | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install playwright beautifulsoup4 pytz | |
| pip install -r requirements.txt | |
| # 5. 安装 Playwright 浏览器 | |
| - name: 安装 Chromium | |
| run: | | |
| python -m playwright install chromium | |
| python -m playwright install-deps | |
| # 6. 运行爬虫 | |
| - name: 执行爬虫脚本 | |
| run: python crawler.py | |
| # 7. 获取生成的文件名 | |
| - name: 获取最新文件名 | |
| id: files | |
| run: | | |
| # 获取最新的 JSON 和 MD 文件 | |
| JSON_FILE=$(ls results_*.json | sort -r | head -n 1) | |
| MD_FILE=$(ls switch_news_*.md | sort -r | head -n 1) | |
| # 设置输出变量供后续步骤使用 | |
| echo "json_file=${JSON_FILE}" >> $GITHUB_OUTPUT | |
| echo "md_file=${MD_FILE}" >> $GITHUB_OUTPUT | |
| echo "找到文件: ${JSON_FILE} 和 ${MD_FILE}" | |
| # 8. 调试输出 | |
| - name: 显示生成内容 | |
| run: | | |
| echo "=== 最新新闻 ===" | |
| cat ${{ steps.files.outputs.md_file }} | |
| echo "\n=== JSON 数据 ===" | |
| jq . ${{ steps.files.outputs.json_file }} | |
| # 9. 提交变更 | |
| - name: 自动提交更新 | |
| run: | | |
| git config --global user.name "GitHub Actions" | |
| git config --global user.email "actions@github.com" | |
| # 添加所有新生成的文件 | |
| git add results_*.json switch_news_*.md | |
| if [ -n "$(git status --porcelain)" ]; then | |
| git commit -m "自动更新: $(date +'%Y-%m-%d %H:%M')" | |
| git push origin HEAD:main | |
| echo "变更已提交" | |
| else | |
| echo "无新内容需要更新" | |
| fi |