Skip to main content

gov-monitor

政府网站通知监测工具。基于 gov-site-list 的 URL 清单,定期轮询各栏目页,发现新通知后落 SQLite。

安装

pip install gov-monitor

使用

跑一轮监测

gov-monitor run

会遍历 gov-site-list 里所有栏目,抓第一页链接,跟数据库里的已有 URL 做 diff,新的入库。

启动 Web 服务

gov-monitor serve notices.db

常驻 HTTP 服务,后台每 5 分钟自动跑一轮采集,暴露 API 供下游(AI 分类、写作)调用。 接口文档见 http://localhost:8000/docs。

查看统计

gov-monitor stats

Docker 部署

在服务器上新建一个目录,放两个文件:

Dockerfile:

FROM python:3.14-slim
RUN pip install --no-cache-dir git+https://github.com/baiyigali/gov-monitor.git
WORKDIR /data
EXPOSE 8000
CMD ["gov-monitor", "serve", "/data/notices.db"]

docker-compose.yml:

services:
  gov-monitor:
    build: .
    container_name: gov-monitor
    restart: always
    ports:
      - "8000:8000"
    volumes:
      - ./data:/data
    environment:
      GOV_MONITOR_INTERVAL: "300"

然后启动:

mkdir -p data
docker compose up -d --build
  • 服务监听宿主机 8000 端口,API 文档在 http://服务器IP:8000/docs
  • SQLite 数据库和状态缓存放 ./data/ 目录,容器删了重建数据不丢
  • 默认每 5 分钟自动采集一轮,改间隔改 docker-compose.yml 里的 GOV_MONITOR_INTERVAL

常用命令:

docker compose logs -f        # 看日志
docker compose restart        # 重启
docker compose down            # 停掉(数据保留在 ./data/)
docker compose up -d --build   # 升级后重新构建

开发

跑测试

pip install -e . pytest

# API 单元测试(快,不发外网请求,必须全过)
python -m pytest gov_monitor/tests/test_api.py -v

# 全量抓取基线测试(慢,会真实访问 136 个政府站,约 1 分钟)
python -m gov_monitor.tests.test_baseline
  • test_api.py:测所有 HTTP 接口(分类回写、写作计数、stats、404),用临时 DB,不依赖外网。
  • test_baseline.py:全量抓取回归测试,对比 baseline.json,检查有没有站从"能抓"退化。退出码 0=过,1=退化。

工作原理

  1. 从 gov-site-list 加载所有栏目 URL
  2. 用 requests 抓每个栏目的第一页 HTML
  3. 用 BeautifulSoup 提取所有 <a> 链接
  4. 过滤:同域名、排除导航/附件/分页链接
  5. 跟 SQLite 里已有的 URL 对比
  6. 新链接入库,字段:url(主键)、title、site、column、first_seen

数据存储

SQLite 文件默认叫 notices.db,也可以自己指定路径:

gov-monitor run /path/to/my.db

表结构很简单,就一张 notices 表:

字段 说明
url 通知详情页 URL(主键,去重用)
title 通知标题
site 站点名称
column_name 栏目名称
first_seen 我们第一次抓到这条通知的时间

技术交流

扫码添加微信,交流使用问题、定制与合作:

微信二维码

项目赞助

本项目由以下微信公众号提供赞助,感谢支持:

「程序员白大力」 —— 法律科技 / 自动化内容创作

程序员白大力公众号二维码

「法啊」 —— 法律科普 / 普法内容

法啊公众号二维码

「极速法考」 —— 法考备考 / 法律职业资格考试

极速法考公众号二维码

License

MIT

Release files for gov-monitor 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gov-monitor 1.0.6
File Size Uploaded
gov_monitor-1.0.6.tar.gz 16.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gov-monitor 1.0.6
File Interpreter ABI Platform
gov_monitor-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 33.3 kB

Release files / gov_monitor-1.0.6.tar.gz

Download URL gov_monitor-1.0.6.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ba00a7d48c1b1ab55cf89692604bee11272d0c5c1dd6702526ba69ccb53ccec3
BLAKE2b-256 checksum
How to use checksums
0b80efbabba783413170d374adebb89f3cf6af89bc5e55ed10603f54810d8880
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release files / gov_monitor-1.0.6-py3-none-any.whl

Download URL gov_monitor-1.0.6-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48f4a23ea8ec8b90fcd98cc78564a7d8de5714dea759fe7340d168ad02b5e346
BLAKE2b-256 checksum
How to use checksums
06c0a4f57316445b6175a3a98dc756bf32cdc7c93b084ba7b84b44de587ddd7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release history Release notifications | RSS feed

1.0.7

2 release files

This release

1.0.6 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page