1 from time import sleep 2 import random,pyperclip,os 3 import pyautogui 4 from openpyxl import load_workbook 5 import datetime 6 import tkinter as tk 7 8 path = os.getcwd()+‘\URL链接.xlsx‘ 9 wb = load_workbook(path) 10 ws = wb.worksheets[0] 11 12 sleep(1) 13 pyautogui.FAILSAFE = True 14 15 width, height = pyautogui.size() 16 print(width,height) 17 18 cur_x, cur_y = pyautogui.position() 19 print(cur_x,cur_y) 20 21 def opation(): 22 def control(url,i,m): 23 pyautogui.click(x = 85, y = 1060, duration=2)#移动鼠标任务栏微信 24 pyautogui.click()#当前位置点击 25 sleep(1) 26 pyautogui.click(x=1063, y=744, duration=2) # 移动鼠标输入框 27 pyautogui.click() # 当前位置点击 28 29 pyautogui.typewrite(url, interval=0.25) # 输入文本 30 pyautogui.press(‘space‘) 31 sleep(1) 32 pyautogui.press(‘enter‘) 33 # pyautogui.click(x=792, y=569, duration=2) # 移动鼠标发送 34 # pyautogui.click() # 当前位置点击 35 sleep(1) 36 pyautogui.click(x=1108, y=626, duration=2) # 移动鼠标点击链接 37 pyautogui.click() # 当前位置点击 38 39 sleep(2) 40 pyautogui.click(x = 1258, y = 300, duration=2)#移动鼠标文章空白位置 41 pyautogui.click()#当前位置点击 42 43 sleep(random.randint(3,6)) 44 pyautogui.hotkey(‘ctrl‘, ‘a‘) # 组合按键(Ctrl+A)#全选 45 sleep(1) 46 pyautogui.hotkey(‘ctrl‘, ‘c‘) # 组合按键(Ctrl+C)#复制 47 sleep(1) 48 text = pyperclip.paste() 49 # print(text) 50 if "阅读 "in text: 51 index = text.index("阅读 ") 52 click = text[index:] 53 for kw in click.split("n"): 54 if "阅读 " in kw: 55 print("阅读:", kw.replace("阅读", "").strip()) 56 # reads.append(kw.replace("阅读", "").strip()) 57 ws.cell(i+1,3+m).value = kw.replace("阅读", "").strip() 58 elif "在看" in kw: 59 print("在看:", kw.replace("在看", "").strip()) 60 sleep(1) 61 pyautogui.click(x=1260, y=150, duration=2) # 移动鼠标"关闭"位置 62 pyautogui.click() # 当前位置点击 #关闭 63 # print(text)8CAA 64 wb.save(path) 65 66 for m in range(int(e1.get())): 67 now_time = datetime.datetime.now().strftime(‘%H:%M:%S‘) # %Y-%m-%d %H:%M:%S 68 ws.cell(1,3+m).value =now_time#监控链接的时间保存 69 for i in range(1,ws.max_row): 70 # print(ws.cell(i+1,2).value) 71 # if len(ws.cell(i+1,2).value): 72 control(ws.cell(i+1,2).value,i,m) 73 74 75 #删除原来的所有图片文件 76 file_names = os.listdir(os.getcwd() + ‘\img\‘) 77 for file_name in file_names: 78 os.remove(os.getcwd() + ‘\img\‘ + file_name) 79 #可视化 80 import matplotlib.pyplot as plt 81 x_time = [] 82 for i in range(3, ws.max_column + 1): 83 x_time.append(ws.cell(1, i).value) 84 print(x_time) 85 for i in range(1, ws.max_row): 86 y_read = [] 87 for j in range(3, ws.max_column + 1): 88 y_read.append(ws.cell(i + 1, j).value) 89 plt.plot(x_time, y_read) 90 plt.title(ws.cell(i + 1, 2).value) 91 plt.savefig(os.getcwd() + ‘\img\‘ + str(i) + ‘.jpg‘) 92 plt.show() 93 94 l2 = tk.Label(win, text=‘监控完成,画图已保存‘) 95 l2.pack() 96 97 if __name__ == ‘__main__‘: 98 win = tk.Tk() 99 win.title(‘监控微信文章阅读量‘)100 win.geometry(‘300x400‘)101 l1 =tk.Label(win,text=‘输入需要监控的多少个时间间隔:‘)102 l1.pack()103 e1 = tk.Entry(win,show=None)104 e1.pack()105 # l2 = tk.Label(win, text=‘输入间隔的时长:(分钟)‘)106 # l2.pack()107 # e2 = tk.Entry(win, show=None)108 # e2.pack()109 #110 b = tk.Button(win,text=‘开始监控‘,width=12,height=1,command=opation)111 b.pack()112 113 win.mainloop()