运行新闻抓取脚本 #3332
This file contains 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: 运行新闻抓取脚本 | |
on: | |
push: | |
branches: | |
- main # 在推送到 main 分支时运行 | |
schedule: | |
- cron: '0 */2 * * *' # 每两小时的第0分钟执行 | |
workflow_dispatch: # 允许手动触发 | |
jobs: | |
setup-and-test: | |
runs-on: ubuntu-latest # 使用 Ubuntu 环境 | |
steps: | |
- name: 检出代码库 | |
uses: actions/checkout@v4 | |
with: | |
persist-credentials: true # 确保能够推送更改 | |
- name: 设置 Python 环境 | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.x' # 指定最新的 Python 3 版本 | |
- name: 设置 Git 配置 | |
run: | | |
git config --global user.name "NowScott" | |
git config --global user.email "[email protected]" | |
- name: 安装 Chrome | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y wget unzip | |
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb | |
sudo dpkg -i google-chrome-stable_current_amd64.deb || sudo apt-get -f install -y | |
- name: 安装 Python 依赖 | |
run: | | |
python -m pip install --upgrade pip | |
pip install selenium webdriver-manager chromedriver-autoinstaller | |
- name: 运行新闻抓取脚本 | |
run: | | |
python src/script.py # 执行新闻抓取脚本,确保脚本路径正确 | |
- name: 提交并推送更改 | |
run: | | |
git add news_archive/ | |
git diff --cached --exit-code news_archive/ || ( | |
DATE=$(TZ="Asia/Shanghai" date "+%Y-%m-%d %H:%M:%S") | |
echo "正在提交更改..." | |
git commit -m "更新新闻文件于 $DATE" | |
git push origin main | |
echo '更新成功' | |
) |