python通過adb串連爬取支付寶移動端的賬單資訊,操作過程如下:
於是此檔案就可以分解為如下四個主要功能:
1.圖片識別;2.類比點擊;3.類比滑動;4.截圖功能
一.對賬單詳情頁的處理:
對賬單詳情頁截圖後,再選擇用圖片分割,捕捉到3個截圖,分別是:名稱欄位,賬單數額欄位;賬單編號欄位;
二.對賬單縮減頁的處理:
如何處理賬單的縮減頁,開始時自己是想,傳入一個基準,然後每次向下遍曆一個等額的數值進行點擊,擷取詳情頁的資料,但是這樣的話,每次只能擷取一頁,而且對遍曆的條件無法得知,於是自己就選擇了讀完一條資料就向下滾動的方法,進行遍曆每一條的賬單;
三.對每月賬單的篩選:
當自己選擇讀完一條資料向下滾動時,此時加上一個判斷條件,即如果接下來截圖並且分割好的圖片中如果含有'月'或者'目'的欄位時,就將其判別為當月結束,跳出迴圈,否則向下滾動,並且類比點擊;
# -*- coding: utf-8 -*-import osimport mathimport matplotlib.pyplot as pltimport pytesseractfrom PIL import Imageimport timeimport csvimport numpy as npimport matplotlib.image as mpimgclass Pay_ali: def __init__(self): self._coefficient = 1.35 self._click_count = 0 self._coords = [] #輸出函數 def print_all(self,text): print(text) print('\n') # 圖片識別 def img_rec(self,img): text = pytesseract.image_to_string(Image.open(img), lang='chi_sim') # print(type(text)) self.print_all(text) return text #點擊功能 def acquire_info(self,x1,y1): x1 = str(x1) y1 = str(y1) os.system('adb shell input tap ' + x1 + ' ' + y1) #滑動功能 def acquire_swipe(self,x1,y1,x2,y2): x1 = str(x1) y1 = str(y1) x2 = str(x2) y2 = str(y2) os.system('adb shell input swipe ' + x1 + ' ' + y1+' '+x2+' '+y2) # 截圖功能 def cut_info(self,path): path = str(path) # screenshot.png. os.system('adb shell screencap -p /sdcard/'+path) os.system('adb pull /sdcard/'+path) # 切分+識別功能 def seg_info(self,x1,y1,x2,y2,path,out_path): path = str(path) out_path = str(out_path) # 此處有不足的情況,截圖會覆蓋 img = Image.open(path) region=(x1,y1,x2,y2) # print(region) cropImg = img.crop(region) cropImg.save(out_path) text = self.img_rec(out_path) return text # 顯示圖片功能 def img_show(self,src): img = Image.open(src) plt.imshow(img) plt.show() # 擷取當月資訊 def acquire_info_month(self,start,end): # start+=262 while (end - start) >= 100: # start += 262 time.sleep(1) self.cut_info('screen2.png') text = self.seg_info(23,start,200,start+250,'screen2.png','crop2.png') if(text.find('月')>=0 or text.find('目')>=0): # self.acquire_swipe(23,start+300,23,start) break else: self.acquire_swipe(23,start+220,23,start) self.acquire_info(240,start+100) time.sleep(1) self.cut_info('screen1.png') time.sleep(1) self.csv_save() time.sleep(1) self.acquire_info(302.8,2100.3) # 將資料儲存成csv def csv_save(self): name = self.seg_info(482, 258, 693, 332, 'screen1.png', 'crop1.png') money = self.seg_info(305, 367, 747, 492, 'screen1.png', 'crop1.png') money_info = self.seg_info(20, 646, 1061, 1709, 'screen1.png', 'crop1.png') # money_info = self.seg_info(20, 646, 1061, 1709, 'screen1.png', 'crop1.png') rows = [(name,money,money_info)] with open('pay_ali.csv','a') as f: f_csv = csv.writer(f) f_csv.writerows(rows) # 運行函數 def run(self): self.acquire_info(972.8,1954.93) self.acquire_info(254,714) self.acquire_info(240,732) time.sleep(1) self.cut_info('screen1.png') time.sleep(1) # self.seg_info(342.7, 364.2, 707.7, 489.6) # self.seg_info(417,258,704,338) self.csv_save() time.sleep(1) self.acquire_info(302.8,2100.3) # self.img_show('crop.png') self.acquire_info_month(475,2023)if __name__ == "__main__": pay_ali = Pay_ali() pay_ali.run() # pay_ali.cut_info('screen_test.png') # pay_ali.img_show('screen_test.png') 整個小demo實現難度不大,現在存在的問題在於如何適配不同的機型,歡迎拍磚~