前言
最近找幾個老友準備聊天發現幾個已經被刪除好友名單,做為潛水黨多年的我已經不知道成為多少人的黑名單,但是好友列表卻依然有不是好友的名單,面對龐大的好友數量想要清除談何容易。雖然可以發消息給所有人,來識別是否是好友,但是畢竟打擾到了其他人,經過一番查詢發現點擊轉賬時會提示不是好友,這里只是點擊轉賬并不是真的轉賬哦。做為一名技術潛水黨,肯定要低調的辦好事情。之前已經用appium玩過自動化了,那么這次就輕車熟路了。
準備
1.Appium環境搭建
環境搭建這里不再介紹,需要的可以看我之前的文章或者百度
2.手動操作流程圖轉自動操作流程圖
最開始畫的流程圖,然后按照流程圖去實現操作流程和邏輯。初步實現完成后進行調試,過程中不少邏輯不嚴謹的地方,以及一些框架自帶的坑,最終總算可以一次性掃描了。但是其中還是存在個別的坑需要手動處理一下。暫時先記錄下來,等以后看情況優化吧。
遇到暫停基本是聊天窗識別不到了,可以手動上滑一下,讓程序識別下一個聊天窗口。需要先登陸微信號。
代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
from appium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time from selenium.common.exceptions import StaleElementReferenceException # platformVersion = input('系統版本號(platformVersion): ') # deviceName = input('設備名稱(deviceName):') desired_caps = { "platformName" : "Android" , # 系統 "platformVersion" : '10.0' , # 系統版本號 # "platformVersion": platformVersion, # 系統版本號 "deviceName" : 'b68548ed' , # 設備名 # "deviceName": deviceName, # 設備名 "appPackage" : "com.tencent.mm" , # 包名 "appActivity" : ".ui.LauncherUI" , # app 啟動時主 Activity 'unicodeKeyboard' : True , # 使用自帶輸入法 'noReset' : True # 保留 session 信息,可以避免重新登錄 } def is_element_exist(driver, by, value): """判斷元素是否存在""" try : driver.find_element(by = by, value = value) except Exception as e: return False else : return True def break_key(n): """點擊返回按鈕""" for i in range (n): el1 = wait.until(EC.element_to_be_clickable((By.ACCESSIBILITY_ID, "返回" ))) el1.click() def swipe_up(): """向上滑動屏幕""" # 獲取屏幕的size size = driver.get_window_size() # 獲取屏幕寬度 width width = size[ 'width' ] # 獲取屏幕高度 height height = size[ 'height' ] x1 = width * 0.5 y1 = height * 0.45 y2 = height * 0.3 driver.swipe(x1,y1,x1,y2, 3000 ) print ( "向上滑動" ) if __name__ = = '__main__' : driver = webdriver.Remote( "http://localhost:4723/wd/hub" , desired_caps) # 設置等待 wait = WebDriverWait(driver, 300 ) status = True n = 2 count = 1 while status: try : # 點擊通訊錄 a1 = wait.until(EC.element_to_be_clickable( (By.XPATH, "//android.widget.FrameLayout[@content-desc=\"當前所在頁面,與的聊天\"]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageView" ))) a1.click() #向上滑動 swipe_up() if n < 13 : # 進入第一個聊天窗口,公眾號為1,用戶元素定位從2開始,一頁最多12,每滑動屏幕從新開始到12. g73 = wait.until(EC.element_to_be_clickable( (By.XPATH, "//android.widget.FrameLayout[@content-desc='當前所在頁面,與的聊天']/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout[%d]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.view.View" % (n)))) g73.click() print ( "進入了第%d個好友聊天窗口" % (count)) count + = 1 else : n - = 1 g73 = wait.until(EC.element_to_be_clickable( (By.XPATH, "//android.widget.FrameLayout[@content-desc='當前所在頁面,與的聊天']/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout/com.tencent.mm.ui.mogic.WxViewPager/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout[%d]/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.view.View" % (n)))) g73.click() print ( "進入了第%d個好友聊天窗口" % (count)) count + = 1 # 判斷聊天窗是否有發送消息的元素 is_weichat = is_element_exist(driver, "id" , "com.tencent.mm:id/ijq" ) if is_weichat = = True : while True : # # 有發消息則點擊 wait.until(EC.element_to_be_clickable( (By. ID , "com.tencent.mm:id/ijq" ))).click() print ( "點擊了發消息" ) #點擊+號 is_jia = is_element_exist(driver, 'id' , 'com.tencent.mm:id/ay7' ) #判斷是否有加號 if is_jia = = True : el4 = wait.until(EC.element_to_be_clickable((By. ID , "com.tencent.mm:id/ay7" ))) el4.click() print ( '點擊+號' ) #判斷是否為轉賬 is_zhuanzhang = wait.until(EC.element_to_be_clickable((By.XPATH, "//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.GridView/android.widget.LinearLayout[6]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView" ))) if is_zhuanzhang.text = = "轉賬" : # is_zhuanzhang = is_element_exist(driver, 'xpath', '//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.GridView/android.widget.LinearLayout[6]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView') # if is_zhuanzhang == True: #點擊轉賬 el5 = wait.until(EC.element_to_be_clickable((By.XPATH, "//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout[1]/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.GridView/android.widget.LinearLayout[6]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.ImageView[2]" ))) el5.click() print ( '點擊轉賬' ) #輸入金額0.01 el6 = wait.until(EC.element_to_be_clickable((By. ID , "com.tencent.mm:id/jf4" ))) el6.send_keys( "0.01" ) print ( '輸入金額' ) #點擊確認轉賬 el7 = wait.until(EC.element_to_be_clickable((By. ID , "com.tencent.mm:id/e6c" ))) el7.click() print ( '點擊確認轉賬' ) time.sleep( 2 ) #判斷是否有知道了 is_not_friend = is_element_exist(driver, 'id' , 'com.tencent.mm:id/ffp' ) if is_not_friend = = True : #點擊知道了 el8 = wait.until(EC.element_to_be_clickable((By. ID , "com.tencent.mm:id/ffp" ))) el8.click() print ( '點擊知道了' ) #獲取用戶名稱并打印 el9 = wait.until(EC.element_to_be_clickable((By. ID , "com.tencent.mm:id/h2k" ))) print ( '不是好友的微信名稱為:' ,el9.text) with open ( 'weixin.txt' , 'a+' )as f: f.write( '不是好友的微信名稱:' + el9.text + '\n' ) driver.keyevent( 4 ) driver.keyevent( 4 ) driver.keyevent( 4 ) driver.keyevent( 4 ) print ( '返回' ) n + = 1 break else : #沒有知道則返回 driver.keyevent( 4 ) break_key( 2 ) n + = 1 print ( '返回' ) break else : #沒有轉賬則返回到首頁 driver.keyevent( 4 ) driver.keyevent( 4 ) print ( '返回' ) n + = 1 break else : #沒有+號則返回到首頁 driver.keyevent( 4 ) driver.keyevent( 4 ) print ( '返回' ) n + = 1 break except StaleElementReferenceException: print ( '捕獲StaleElementReferenceException異常' ) |
這里已經掃描到200多個好友了,其中可能需要手動上滑一下
不是好友的名單會在當前目錄生成一個txt文件進行保存
偶然出現幾個異常,不知道是什么原因
總的來說功能基本都已經實現了,還有細節問題后面看情況優化吧
到此這篇關于Python自動掃描出微信不是好友名單的方法的文章就介紹到這了,更多相關Python自動掃描出微信好友內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/huny/p/14788006.html