import pywikibot
import re
import os
import pymysql
from datetime import datetime, timedelta
site = pywikibot.Site('zh', 'wikipedia')
site.login()
host = os.environ['MYSQL_HOST']
user = os.environ['MYSQL_USERNAME']
password = os.environ['MYSQL_PASSWORD']
conn = pymysql.connect(
    host=host,
    user=user,
    password=password,
    charset="utf8"
)
d = datetime.today() + timedelta(days=30)
with conn.cursor() as cur:
    cur.execute('use zhwiki_p')
    cur.execute("""
        SELECT page_title
        FROM page
        WHERE page_title REGEXP "(数|數)$"
            AND page_namespace = 0
        ORDER BY page_title ASC
    """)
    res = cur.fetchall()
text = """[[Special:链出更改/{{FULLPAGENAME}}|檢查最近更改]]
"""
for row in res:
    title = row[0].decode()
    text += "\n# [[{}]]".format(title)
page = pywikibot.Page(site, "User:Xiplus/沙盒5")
page.text = text
page.save(summary="更新", minor=False)
Page [[zh:User:Xiplus/沙盒5]] saved