惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
V
Visual Studio Blog
C
Check Point Blog
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
量子位
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
M
MIT News - Artificial intelligence
爱范儿
爱范儿
B
Blog RSS Feed
MyScale Blog
MyScale Blog
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
L
LangChain Blog
D
Docker

流动

山西之旅 | 流动 博客焕新,记录继续 | 流动 友链 | 流动 博客加速实践 广府古城一日游 我做了一个口播短视频二创工具:VideoRemaker Windows 下 Codex Cli 更新报 Move-Item is denied 错误的解决 Caddy 转发 GOST 报 TLS internal error 问题的排查与解决 对口型视频合成方案对比:Wav2Lip、VideoReTalking 与 MuseTalk 2026 音色克隆方案对比:IndexTTS-2、CosyVoice、GPT-SoVITS、Fish Speech、VoxCPM 部署与实测 AI Agent折腾记(OpenClaw / Hermes Agent) 我的2025年 大连之行 回家收麦 六一儿童节爬长城 Golang database/sql 数据库断线自动重连机制解析 Golang默认Http Client导致的cannot assign requested address错误 清明踏春,爬山看海 购入小牛G400T电动车 北京的三月飞雪 wrenAI本地LLM模型部署 天津一日游 2024年终总结 停止使用staticfile.org服务 使用 ImageMagick 自动添加水印,保护图片版权 如何注册一个.sol域名 奥森公园半日游 昌平42公里骑行绿道打卡 十月一日爬慕田峪长城 当Hugo遇上AVIF,优化图片加载
利用Github Actions定时抓取微博
Liudon · 2023-10-07 · via 流动

背景

在微博上关注了一些用户,比如tk教主月风

但是有些内容过段时间不可见了,所以希望可以定时抓取微博归档备份下来。

实现方案

整体思路:利用Github ActionsScheduled任务,定时执行抓取shell脚本,将内容保存到文件,提交到Github仓库。

  1. 新建仓库,比如weibo_archive

  2. 添加抓取脚本,完整代码

    这里用到微博两个接口:

    // 抓取某个用户最新的10条微博数据,返回里有每条微博的id,这里如果内容过长的话会被截断
    https://m.weibo.cn/api/container/getIndex?jumpfrom=weibocom&type=uid&value=$uid&containerid=107603$uid
    
    // 根据微博id,抓取微博完整的内容
    https://m.weibo.cn/statuses/extend?id=$id
    
  3. 添加环境变量。

    • 进入个人设置->Developer Settings->Personal access tokens->Tokens (classic),创建新的Token,记下对应的值。

    • 进入第一步创建仓库的配置页,点击Secrets and variables下的Actions

      切到Secret目录,创建新的Secret变量,名称为TOKEN,值为前一步记录的值;切到Variables目录,创建新的Variables变量,名称为WEIBO_UIDS,值为你需要抓取的微博用户id,多个用户的话以|分割。

  4. 添加定时任务,完整yaml文件如下。

    # This is a basic workflow to help you get started with Actions
    
    name: CI
    
    # Controls when the workflow will run
    on:
    # Triggers the workflow on push or pull request events but only for the "main" branch
    push:
        branches: [ "main" ]
    pull_request:
        branches: [ "main" ]
    
    # Allows you to run this workflow manually from the Actions tab
    workflow_dispatch:
    schedule:
        - cron: '*/5 * * * *'
    
    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
    # This workflow contains a single job called "build"
    build:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
    
        permissions:      
        contents: write
    
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
        # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
        - uses: actions/checkout@v3
    
        # Runs a single command using the runners shell
        - name: archive weibo
            run: |
            chmod +x ./weibo_archive.sh
            ./weibo_archive.sh
            shell: bash
            env:
            weibo_uids: ${{ vars.weibo_uids }}
    
        # Runs a set of commands using the runners shell
        - name: Commit changes
            uses: EndBug/add-and-commit@v9
            env:
            github_token: ${{ secrets.TOKEN }}
            add: .
    

效果

抓取后的内容,会按用户id分别保存到不同文件。

效果

不过这个方案有一个唯一的缺点,Github Actions定时任务时间粒度最小是5分钟,而且不能保证执行时间完全符合这个粒度,所以可能还是会有漏掉的内容。

Note: The schedule event can be delayed during periods of high loads of GitHub Actions workflow runs. High load times include the start of every hour. If the load is sufficiently high enough, some queued jobs may be dropped. To decrease the chance of delay, schedule your workflow to run at a different time of the hour.