Python automatically scans wechat's list of non friends
Huny 2021-06-04 10:57:24

Preface

Recently, I found some old friends ready to chat, and found that some of them have been deleted , As a diver for many years, I don't know how many people have been blacklisted , But there is still a list of friends who are not friends , In the face of a huge number of friends, it's hard to get rid of them . Although you can send messages to everyone , To identify if you're a friend , But after all, it bothers other people , After some inquiry, it is found that when you click transfer, you will be prompted that you are not a friend , This is just click transfer, not real transfer . As a tech diver , We must do things well in a low profile . It has been used before appium I've played automation , Then I'll be familiar with it this time .

Get ready

1.Appium Environment building
Environment construction will not be introduced here , Need to see my previous article or Baidu
2. Manual operation flow chart to automatic operation flow chart

The initial flow chart , Then according to the flow chart to realize the operation process and logic . After the completion of the preliminary implementation, debug , There are a lot of loose logic in the process , And some of the holes in the frame , Finally, it can be scanned at one time . But there are still individual pits that need to be handled manually . Record it for the time being , Let's see the situation later .
Encounter pause is basically chat window can't recognize , You can slide up by hand , Let the program recognize the next chat window . You need to log in to wechat first .

Code

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(' System version number (platformVersion): ')
# deviceName = input(' Equipment name (deviceName):')
desired_caps = {
"platformName": "Android", # System
"platformVersion": '10.0', # System version number
# "platformVersion": platformVersion, # System version number
"deviceName": 'b68548ed', # Device name
# "deviceName": deviceName, # Device name
"appPackage": "com.tencent.mm", # Package name
"appActivity": ".ui.LauncherUI", # app When it starts, it's mainly Activity
'unicodeKeyboard': True, # Use your own input method
'noReset': True # Retain session Information , You can avoid logging back in
}
def is_element_exist(driver, by, value):
""" Determine whether an element exists """
try:
driver.find_element(by=by, value=value)
except Exception as e:
return False
else:
return True
def break_key(n):
""" Click the back button """
for i in range(n):
el1 = wait.until(EC.element_to_be_clickable((By.ACCESSIBILITY_ID," return ")))
el1.click()
def swipe_up():
""" Slide the screen up """
# Get the... Of the screen size
size = driver.get_window_size()
# Get screen width width
width = size['width']
# Get screen height height
height = size['height']
x1 = width*0.5
y1 = height*0.45
y2 = height*0.3
driver.swipe(x1,y1,x1,y2,3000)
print(" You slide ")
if __name__ == '__main__':
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# Set the wait
wait = WebDriverWait(driver, 300)
status = True
n = 2
count = 1
while status:
try:
# Click Address Book
a1 = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//android.widget.FrameLayout[@content-desc=\" The current page , Chat with \"]/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()
# You slide
swipe_up()
if n < 13:
# Enter the first chat window , The official account is 1, User element positioning from 2 Start , One page is the most 12, Every slide of the screen starts from scratch to 12.
g73 = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//android.widget.FrameLayout[@content-desc=' The current page , Chat with ']/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(" Entered the first place %d A friend chat window "%(count))
count += 1
else:
n -= 1
g73 = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//android.widget.FrameLayout[@content-desc=' The current page , Chat with ']/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(" Entered the first place %d A friend chat window "%(count))
count += 1
# Determine whether the chat window has elements to send messages
is_weichat = is_element_exist(driver, "id", "com.tencent.mm:id/ijq")
if is_weichat == True:
while True:
# # If you have a message, click
wait.until(EC.element_to_be_clickable(
(By.ID, "com.tencent.mm:id/ijq"))).click()
print(" Click to send a message ")
# Click on + Number
is_jia = is_element_exist(driver, 'id', 'com.tencent.mm:id/ay7')
# Judge whether there is a plus sign
if is_jia == True:
el4 = wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/ay7")))
el4.click()
print(' Click on + Number ')
# Judge whether it is transfer
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 == " Transfer accounts ":
# 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:
# Click transfer
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(' Click transfer ')
# Input amount 0.01
el6 = wait.until(EC.element_to_be_clickable((By.ID,"com.tencent.mm:id/jf4")))
el6.send_keys("0.01")
print(' Input amount ')
# Click confirm transfer
el7 = wait.until(EC.element_to_be_clickable((By.ID,"com.tencent.mm:id/e6c")))
el7.click()
print(' Click confirm transfer ')
time.sleep(2)
# Judge if anyone knows
is_not_friend = is_element_exist(driver,'id','com.tencent.mm:id/ffp')
if is_not_friend == True:
# Click to see
el8 = wait.until(EC.element_to_be_clickable((By.ID,"com.tencent.mm:id/ffp")))
el8.click()
print(' Click to see ')
# Get the user name and print
el9 = wait.until(EC.element_to_be_clickable((By.ID,"com.tencent.mm:id/h2k")))
print(' The wechat name that is not a friend is :',el9.text)
with open('weixin.txt','a+')as f:
f.write(' Not a friend's wechat name :' + el9.text + '\n')
driver.keyevent(4)
driver.keyevent(4)
driver.keyevent(4)
driver.keyevent(4)
print(' return ')
n += 1
break
else:
# If you don't know, return to
driver.keyevent(4)
break_key(2)
n += 1
print(' return ')
break
else:
# If there is no transfer, return to the home page
driver.keyevent(4)
driver.keyevent(4)
print(' return ')
n += 1
break
else:
# No, + No. returns to the home page
driver.keyevent(4)
driver.keyevent(4)
print(' return ')
n += 1
break
except StaleElementReferenceException:
print(' Capture StaleElementReferenceException abnormal ')

It's scanned here 200 More than one friend , You may need to slide up manually

The list of non friends will be generated in the current directory txt Keep the file

A couple of anomalies happened by accident , I don't know why

In general, the functions have been basically realized , There are also details. Let's see the optimization later

Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09