Python一鍵生成核酸檢測(cè)日歷的操作代碼
大家好,我是小小明。鑒于深圳最近每天都要做核酸,我覺(jué)得有必要用程序生成自己的檢測(cè)日歷,方便查看。
首先我們需要從深i您-自主申報(bào)
的微信小程序中提取自己的核酸檢測(cè)記錄,然后使用繪圖庫(kù)自動(dòng)繪制檢測(cè)日歷。
UI自動(dòng)化提取檢測(cè)記錄
首先,我們?cè)赑C端微信打開深i您-自主申報(bào)
微信小程序:
點(diǎn)擊 核酸檢測(cè)記錄,再點(diǎn)擊自己的姓名即可查看自己的核酸檢測(cè)記錄:
下面我們打開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 = {"檢測(cè)結(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.采樣時(shí)間 = pd.to_datetime(df.采樣時(shí)間) df.檢測(cè)時(shí)間 = pd.to_datetime(df.檢測(cè)時(shí)間) df.head()
結(jié)果如下:
有了檢測(cè)數(shù)據(jù),我們就可以生成檢測(cè)日歷了。也可以先將檢測(cè)數(shù)據(jù)保存起來(lái):
df.to_excel(f"{pd.Timestamp('now').date()}核酸檢測(cè)記錄.xlsx", index=False)
注意:其他省市的童鞋,請(qǐng)根據(jù)自己城市對(duì)應(yīng)的小程序?qū)嶋H情況編寫代碼進(jìn)行提取。
原本想抓包獲取檢測(cè)數(shù)據(jù),卻發(fā)現(xiàn)新版的PC端微信的小程序已經(jīng)無(wú)法被抓包,暫時(shí)還未理解啥原理啥實(shí)現(xiàn)的。
后面碰到正在檢測(cè)的記錄上面的代碼會(huì)報(bào)錯(cuò),分析節(jié)點(diǎn)后,下面升級(jí)到能夠兼容出現(xiàn)檢測(cè)記錄的情況:
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 = {"檢測(cè)結(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 = {"檢測(cè)結(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.采樣時(shí)間 = pd.to_datetime(df.采樣時(shí)間) df.檢測(cè)時(shí)間 = pd.to_datetime(df.檢測(cè)時(shí)間) df.head()
生成核酸檢測(cè)日歷
經(jīng)過(guò)幾小時(shí)的測(cè)試,最終編寫出如下方法:
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) # 第一天是星期幾和一個(gè)月的天數(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個(gè)月的核酸檢測(cè)日歷:
dates = df.采樣時(shí)間.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}月檢測(cè)記錄.jpg")
到此這篇關(guān)于Python一鍵生成核酸檢測(cè)日歷的文章就介紹到這了,更多相關(guān)Python生成核酸檢測(cè)日歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python抓取豆瓣圖片并自動(dòng)保存示例學(xué)習(xí)
python抓取豆瓣圖片并自動(dòng)保存示例學(xué)習(xí),示例使用了beautifulsoup庫(kù)分析HTML代碼,beautifulsoup是一個(gè)HTML/XML解析器,可以用來(lái)做網(wǎng)頁(yè)爬蟲2014-01-01pycharm中django框架連接mysql數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了pycharm中django框架連接mysql數(shù)據(jù)庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04nginx搭建基于python的web環(huán)境的實(shí)現(xiàn)步驟
這篇文章主要介紹了nginx搭建基于python的web環(huán)境的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python實(shí)現(xiàn)的調(diào)用C語(yǔ)言函數(shù)功能簡(jiǎn)單實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的調(diào)用C語(yǔ)言函數(shù)功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了Python使用ctypes模塊調(diào)用C語(yǔ)言函數(shù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-03-03python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解
這篇文章主要為大家介紹了python控制結(jié)構(gòu)的條件判斷與循環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06python在Windows8下獲取本機(jī)ip地址的方法
這篇文章主要介紹了python在Windows8下獲取本機(jī)ip地址的方法,涉及Python中socket包相關(guān)函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03Django實(shí)現(xiàn)在線無(wú)水印抖音視頻下載(附源碼及地址)
該項(xiàng)目功能簡(jiǎn)單,完全復(fù)制SaveTweetVedio的項(xiàng)目。用戶觀看抖音視頻時(shí)選擇復(fù)制視頻鏈接,輸入到下載輸入欄,即可下載無(wú)水印視頻,還可掃描二維碼手機(jī)上預(yù)覽。親測(cè)成功。2021-05-05