Python一鍵生成核酸檢測日歷的操作代碼
大家好,我是小小明。鑒于深圳最近每天都要做核酸,我覺得有必要用程序生成自己的檢測日歷,方便查看。
首先我們需要從深i您-自主申報(bào)的微信小程序中提取自己的核酸檢測記錄,然后使用繪圖庫自動繪制檢測日歷。
UI自動化提取檢測記錄
首先,我們在PC端微信打開深i您-自主申報(bào)微信小程序:

點(diǎn)擊 核酸檢測記錄,再點(diǎn)擊自己的姓名即可查看自己的核酸檢測記錄:

下面我們打開inspect.exe工具分析查看節(jié)點(diǎn):

于是開始編碼:
import pandas as pd
import uiautomation as auto
pane = auto.PaneControl(searchDepth=1, Name="深i您 - 自主申報(bào)")
pane.SetActive(waitTime=0.01)
pane.SetTopmost(waitTime=0.01)
tags = pane.GetFirstChildControl().GetFirstChildControl() \
.GetLastChildControl().GetFirstChildControl() \
.GetChildren()
result = []
for tag in tags:
tmp = []
for item, depth in auto.WalkControl(tag, maxDepth=4):
if item.ControlType != auto.ControlType.TextControl:
continue
tmp.append(item.Name)
row = {"檢測結(jié)果": tmp[1]}
for k, v in zip(tmp[2::2], tmp[3::2]):
row[k[:-1]] = v
result.append(row)
pane.SetTopmost(False, waitTime=0.01)
df = pd.DataFrame(result)
df.采樣時間 = pd.to_datetime(df.采樣時間)
df.檢測時間 = pd.to_datetime(df.檢測時間)
df.head()
結(jié)果如下:

有了檢測數(shù)據(jù),我們就可以生成檢測日歷了。也可以先將檢測數(shù)據(jù)保存起來:
df.to_excel(f"{pd.Timestamp('now').date()}核酸檢測記錄.xlsx", index=False)
注意:其他省市的童鞋,請根據(jù)自己城市對應(yīng)的小程序?qū)嶋H情況編寫代碼進(jìn)行提取。
原本想抓包獲取檢測數(shù)據(jù),卻發(fā)現(xiàn)新版的PC端微信的小程序已經(jīng)無法被抓包,暫時還未理解啥原理啥實(shí)現(xiàn)的。
后面碰到正在檢測的記錄上面的代碼會報(bào)錯,分析節(jié)點(diǎn)后,下面升級到能夠兼容出現(xiàn)檢測記錄的情況:
import pandas as pd
import uiautomation as auto
pane = auto.PaneControl(searchDepth=1, Name="深i您 - 自主申報(bào)")
pane.SetActive(waitTime=0.01)
pane.SetTopmost(waitTime=0.01)
tag = pane.GetFirstChildControl().GetFirstChildControl() \
.GetLastChildControl().GetFirstChildControl()
tmp = []
for item, depth in auto.WalkControl(tag, maxDepth=2):
if item.ControlType != auto.ControlType.TextControl:
continue
tmp.append(item.Name)
result = []
tags = tag.GetChildren()
if tmp:
row = {"檢測結(jié)果": tmp[1]}
for k, v in zip(tmp[2::2], tmp[3::2]):
row[k[:-1]] = v
result.append(row)
for tag in tags:
if tag.Name or tag.GetFirstChildControl().Name:
continue
tmp = []
for item, depth in auto.WalkControl(tag, maxDepth=4):
if item.ControlType != auto.ControlType.TextControl:
continue
tmp.append(item.Name)
row = {"檢測結(jié)果": tmp[1]}
for k, v in zip(tmp[2::2], tmp[3::2]):
row[k[:-1]] = v
result.append(row)
pane.SetTopmost(False, waitTime=0.01)
df = pd.DataFrame(result)
df.采樣時間 = pd.to_datetime(df.采樣時間)
df.檢測時間 = pd.to_datetime(df.檢測時間)
df.head()

生成核酸檢測日歷
經(jīng)過幾小時的測試,最終編寫出如下方法:
import calendar
from PIL import Image, ImageFont, ImageDraw
def create_calendar_img(year, month, days):
"作者:小小明 https://xxmdmst.blog.csdn.net"
font = ImageFont.truetype('msyh.ttc', size=20)
im = Image.new(mode='RGB', size=(505, 251), color="white")
draw = ImageDraw.Draw(im=im)
draw.rectangle((0, 0, 504, 40), fill='#418CFA', outline='black', width=1)
draw.text(xy=(170, 0), text=f"{year}年{month}月", fill=0,
font=ImageFont.truetype('msyh.ttc', size=30))
title_datas = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
for i, title_data in enumerate(title_datas):
draw.rectangle((i*72, 40, (i+1)*72, 70),
fill='lightblue', outline='black', width=1)
draw.text(xy=(i*72+7, 40), text=title_data, fill=0,
font=font)
# 第一天是星期幾和一個月的天數(shù)
weekday, day_num = calendar.monthrange(year, month)
col, row = weekday, 1
for i in range(1, day_num+1):
if col >= 7:
col = 0
row += 1
fill = "#009B3C" if i in days else None
draw.rectangle((col*72, 40+30*row, (col+1)*72, 40+30*(row+1)),
fill=fill, outline='black', width=1)
draw.text(xy=(col*72+24, 40+30*row), text=str(i).zfill(2), fill=0,
font=font)
col += 1
return im
然后我們可以生成最近1-3個月的核酸檢測日歷:
dates = df.采樣時間.dt.date.sort_values().astype(str)
for month, date_split in dates.groupby(dates.str[:7]):
year, month = map(int, month.split("-"))
days = date_split.str[-2:].astype(int)
im = create_calendar_img(year, month, days.values)
display(im)

可以看到我最近連續(xù)9天都是每天做一次核酸。
如果有需要,這些圖片也可以按需保存:
im.save(f"{year}年{month}月檢測記錄.jpg")
到此這篇關(guān)于Python一鍵生成核酸檢測日歷的文章就介紹到這了,更多相關(guān)Python生成核酸檢測日歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python抓取豆瓣圖片并自動保存示例學(xué)習(xí)
python抓取豆瓣圖片并自動保存示例學(xué)習(xí),示例使用了beautifulsoup庫分析HTML代碼,beautifulsoup是一個HTML/XML解析器,可以用來做網(wǎng)頁爬蟲2014-01-01
pycharm中django框架連接mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了pycharm中django框架連接mysql數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
nginx搭建基于python的web環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了nginx搭建基于python的web環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python實(shí)現(xiàn)的調(diào)用C語言函數(shù)功能簡單實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的調(diào)用C語言函數(shù)功能,結(jié)合簡單實(shí)例形式分析了Python使用ctypes模塊調(diào)用C語言函數(shù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解
這篇文章主要為大家介紹了python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
python在Windows8下獲取本機(jī)ip地址的方法
這篇文章主要介紹了python在Windows8下獲取本機(jī)ip地址的方法,涉及Python中socket包相關(guān)函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03
Django實(shí)現(xiàn)在線無水印抖音視頻下載(附源碼及地址)
該項(xiàng)目功能簡單,完全復(fù)制SaveTweetVedio的項(xiàng)目。用戶觀看抖音視頻時選擇復(fù)制視頻鏈接,輸入到下載輸入欄,即可下載無水印視頻,還可掃描二維碼手機(jī)上預(yù)覽。親測成功。2021-05-05

