定时访问网站

"GitHub Action 实现"

Posted by xuhao on June 17, 2024

“省略掉非必需的字,这样读者就会注意到你想保留的。 ”

定时访问网站

一、GitHub Action代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
name: Auto Visit Websites

on:
  schedule:
    - cron: '0 */12 * * *'  # 每12小时执行一次
  workflow_dispatch:

jobs:
  auto-visit-websites:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.x

    - name: Install requests library
      run: pip install requests

    - name: Auto Visit Websites
      run: |
        echo "
        import requests

        websites = [
            'https://jerryz.com.cn',
            'https://blog.jerryz.com.cn'
            # 添加更多网站
        ]

        def check_website(url):
            try:
                response = requests.get(url)
                if response.status_code == 200:
                    print(f'Website {url} is up.')
                else:
                    print(f'Website {url} returned status code {response.status_code}.')
            except requests.exceptions.RequestException as e:
                print(f'An error occurred while checking {url}: {e}')

        for website in websites:
            check_website(website)
        " > auto_visit_websites.py
        python auto_visit_websites.py

二、使用方法

1.创建你的GitHub个人访问令牌(PAT)

请生成拥有 repo 功能的token

2.使用方法

请修改第5行的cron变量(执行时间)及29行的 websites 变量(访问网站)。

tip: 该代码由 Jerry Zhou 提供。